how to send Image bytes using query string - c#

I am new in developing Rest service using web api in asp .net .
I try to send image byte with in the query string.but i could not abe to send image byte with in query string.
I have Basic concept in GET And Post.
Post don't have any limitation to send data.
Herewith i have added my sample code also.
[AcceptVerbs("GET", "POST")]
public string PostRoute(string routingRequestTemplate)
{
return routingRequestTemplate;
}
My string value:
hjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjsgbYwjolIo2s2YoqTF4YeSWSmTLmm1UNwuk/W4340Jm2jmTwLsRptpFkMNmgcKaZtZse1SAoSdfm9u0MpAMXgj5ZWHk3KqEs4qRrQQYcjfpyFab3HSqajJ4DCwIXu7NCBwV9SrAUKRr8DR4poJ3M0zFo6m9EUF+uA5uFdlKG7w9118kehITj2k1N+m8kJl2riTFI3iO8gjOUfHyQrVw32E3Mg+yW0pF3AuJ6L2sV7s5vHzu19066VN9I/DyBTwqXl45u1QaolyD0Ufw7OFcK+A4KT6C5mP7v5ew+dTYh1CeB/i5Vr5P5MEYvApkC3jM5UmzgJ+lM7uYGj+VztNO4Khyl3qG8OPQOH4dncAPXP95ka/lfflcrIAKujLNxH0p3/ej4Ekur4bNAq3dXGtr/bs8wI2K3FS6r3pHUHp/Gp3PXmD89QImF5Yw/s8iup6+4PsP8TU/F/gCX8pFzMTvkQweg4pQhQou8iSXxwXenuZOy7fB+wmdt30AFxhERSanMbf2Blu7u9jbA8eeGtt8Pbu6htDENE3tEPwyeTa4IU1xK04CT0ytrDhzsktFIltVOsvEOyuF7yxuhIt+rJsqdm38AR4sLmNnl8QZ/Nva2cX4/CJ+H5lUvp+HptcubkhaATwNPGmPcje1EzyWzlK4iBl5DFWumOAFuHtC08wcVjc2DJA7eIs8vl5/j4ZHs2o+XDELz4uZ1/tE8JhOEcVzRBq5/oIpFV6YjIQqhckTX62YAImZjT55hrXNzRPB26XZXSGYsbnnuMSNBV1USVG8sw6rzmXKv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Wc8REZOTyLOi7dHFXyrTJ/s5c8iA5Xg+EZCOo+pGUm3GH+PbT9TBo8uyY5GUS6X/S1P40a644++YfQweHjO/lvycCM1PBdWV1Azdl/ZU3KCkgu0o23/dU3WwKul4gXgrmmELxRjFGavoTPDMAKffBe3K5Hudx19CDBqnXz5Gi+XV7C0uoa1JOkWswjKccbnX+LCwDBzgB10tw6ATuxrCjxGtMrNMp2yBR4j2wJGgcbNstOngKeUj309I+F7lHm6c1S/AF1i//Qspl8tYJkASkplTRoBktyf2Sbbr3C/ifm/VQrmeEuPOpcj3GzK4HH0Is/G4BkPkVI+Ppl+PmAS7brpfo+39OIX5t9KmYsL359Cx8Mn6JuewdDsHEafvWCfMHG7y9/LdoMzc2iZfIKrg3dZbEDoCLn0Kx0RWFgGj0NmOWy5LI/yMqWiIlubjF4YsO30aQCYz76fl/DJiEOeSvh2EsRunKQrPtNzG7/0DiRtv21uc5qJ6mO0nSSu5ViFTB6r88hNcUIz7WqZPM4pE/ACqjzKWxd1FHgChcAhqiSgSDmU9AGluEC+S/JZIDLTZH/pO3qporKf4V41eKoQdHPkQvJ40sfbATxVHuUAxduuQAKHAYi4RUn4ShGoURhgFAok+5TxWQHOONb2czjiu2nFkxwe1U7yeDJs5mF5lE+qVHgARxhqB/e31R9zsA1SvvemwVOKR/BuETxWIquUCqtU7FaNnKohDSU0+5nqeWyzX0rglRO8aubyNosFbGOMHVRNX9seBTtJwZPSd1WLJ8HFpuIRPA9TKr4gI1v2VfTN2aObY+cHIWXwqjR4+oHbxQNnGrzN4CKXwYVb+ngET6pU/EwJqLFFh0W3GrpdQCdKbhm8CoLHqNYtoxd1ETX/QoO3y5tgZ5ca79pSBk8pHmecsVhAg6fBs+wBrIHH4TKleA3wEDxvDXN5XFlAJq6oyS3x6Nb/rwOwdxlIC3gcAtLgadWzpHqpg0
url:
http://localhost:64528/api/controller/PostRoute?routingRequestTemplate=string
following error I got
Request filtering is configured on the Web server to deny the request because the query string is too long.

Querystring have limition of size, below is details
Maximum length of Querystring is based on the browser not depend upon the asp.net.
Maximum length of a querystring in IE 4.0 and above is ~2048 characters
IE. 4,5,6,7, - 2,048 characters.
Opera supports - 4050 characters.
Netscape 6 supports - 2000 characters.
Firefox Supports - 6000 characters
Try below codes with Post verb :
[HttpPost]
public ActionResult PostRoute(HttpPostedFileBase file) {
}
this link will also help you :
Uploading image in ASP.NET MVC

What is the maximum possible length of a query string?
Your image will need to be quite small, and base64 encoded, or made url safe. URL length is quite limited. I would suggest putting it in a POST body.
http://en.wikipedia.org/wiki/Base64

Related

How to prevent default url decoding in ASP.net core

In my controller I have an HTTP get method that accepts a string
[HttpGet("{token}"]
public async Task<IActionResult> GetFoo(string token)
{
//Some actine
return Ok(object);
}
If I send the below-encoded token test%2Atest, ASP .NET will decode this token to test*test by default. But if I send test%2Ftest, it does not decode the %2F to /.
I can understand why ASP.NET doesn't do that as it breaks the routes.
Is there a way to disable this default behavior so I can de the decoding in my controller?
Several test results in my side, but I don't think it's what you want.
I think if you can use this kind of parameter-receiving method?
https://localhost:44393/api/hello?token=test%2FParam
[HttpGet]
public string Index(string token)
{
return token;
}
I could not find a way to disable default URL decoding in ASP.net core.
As Tiny Wang suggested above, I changed the application to send the token in the header and that fixed the problem.
For anyone facing something similar, looking for a more "elegant" solution: just base64 encode the parameter before the request and decode it when you have to use it. I faced the same issue when sending an auto-generated email confirmation token through email and later an http request, and enconding it in base64 and decoding it when using it did the trick just fine.

Returning Images from WebApi

I am making a Web Api and I need to return multiple images to the client. I am currently using a Base64 string for each image but this results in each request taking far too long. Is there a better, more efficient way of returning images?
This is the code I am using:
Controller:
public LearningUnits GetLearningUnits([FromODataUri]Guid key)
{
var record = db.LearningUnits.SingleOrDefault(inst => inst.LearningUnitId == key);
record.ImageURLPath = ImageHandler.ImageToByteArrayFromFilePath(record.ImageURLPath);
return record;
}
ImageToByteArray Method:
public static string ImageToByteArrayFromFilePath(string imagefilePath)
{
byte[] imageArray = File.ReadAllBytes(imagefilePath);
string baseImage = Convert.ToBase64String(imageArray);
return baseImage;
}
If the endpoint returns json, then there is no other way, beyond base64, how to embed binaries within the response. But it is definitely a bad idea due to the performance issues. May be for some icons it would be ok, but for larger images its not suitable.
So the best solution here, is to return the url to the image. And the client will make further request to get raw bytes for the image.
Also worth to mention, the image url can not only be the path to the static file, but also a path to some webapi endpoint, which, for instance, gets the image bytes by the resource id and sends the client raw binary back, and not a json string.
The solution is to use media formatters, so that when the Web API is queried with a particular type, you'll then receive the actual binary stream of the particular type.
http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
This is the standard RESTful pattern; you have the same URL, but when you want a JSON record for the data, you accept-type:application/json, but you change your MIME request type to image/png or something similar when you want that media type.
More can be found here:
How to provide custom mediatype formats for OData Api
Odata also implicitly supports it here: https://msdn.microsoft.com/en-us/library/system.web.http.odata.formatter.odatamediatypeformatter(v=vs.118).aspx

Url Params not accepting lengthy strings

While sending the image to Web server along with data it is not accepting the lengthy strings. I am sending the data through url. its working good with smaller strings for country,continent and city
http://user.co/UserImage.svc/InsertObjectImage?UserId={UserId}&CategoryId={CategoryId}&ImageName={ImageName}&Gender={Gender}&Continent={Continent}&Country={Country}&City={City}
The above url am using in program upto "?", after that it is with params
NSString *url=[NSString stringWithFormat:#"http://userdata.co.in/UserImage.svc/InsertFacialImage?%#",requestString];
NSLog(#"insert facial image url : %#",url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
The code I am using to send the image along with data.Here am passing strings with url.Objective-C NSString can hold the data up to 4.2Billion characters. In the web server the I made it to allow 200 characters for param. But when I am sending the lengthy string like united states of america its making the trouble not storing the data.Services developed in WCF using C#
There are length limitations to URLs but it is also worth checking that your URL is valid.
URLs need to be correctly encoded so you should be passing united%20states%20of%20america in your URL string rather than united states of america.
This is because URL encodes spaces to %20. For any other "special" characters these are also encoded. There are many online resource that should help you and a quick online encoder / decoder can be found here: http://meyerweb.com/eric/tools/dencoder/

What characters are html encoded?

TL;DR Düsseldor is encoded to D&#252;sseldor, Düsseldorf isn't encoded (by the default MVC encoding) why?
I'm building an api with soap access. When I get a request I html encode all the data.
Just before I end the current request I copy the Response.Stream and save it for logging.
I noticed that Düsseldorf is encoded by my api to "Düsseldorf". This is expected.
But in my logs it shows Düsseldorf encoded as "D&#252;sseldorf" ( double encoded ) my logs saves after the MVC serializes writes to the response stream.
I think this is because soap encodes values before sending them over the wire.
So my next test was to turn off html encoding in my api.
So this time Düsseldorf was giving to the soap serializer.
But it was encoded as Düsseldorf?
EDIT: When I say I turn on / off the encoding what I mean is each field in the request I use HttpUtility.HtmlEncode("some field"). And save that to the database. Then I return the data with an MVC view like so:
public ActionResult CreateAccessCode ( CreateAccessCodeRequest request )
{
var response = new Response (request);
return new ContentResult (response);
}
My question is when the ContentResult method runs, it seems to do some encoding of its own and I was wondering what that was?
SOAP uses XML - HTML and XML require slightly different encoding, but it may or may not be a problem in your case.
Much more likely your manual construction of XML is not correct. Please do not construct XML with string concatenation - XDocument/XmlDocument are much more suitable for that.

Get a bad request when try to use .getJSON to call ASP.Net MVC 3 json method

my client using jQuery 1.7 try to make a request to my server using ASP.Net MVC 3 Json method. I will give a shot of my request like this below:
GET http://www.domain.com/GetReq?allMounted=&clusterMounted=&ids=30610270%3B518443402%3B524472053%3B526202914%3B530431939%3B532547899%3B536637280%3B541356006%3B545440588%3B554550099%3B555233306%3B563618755%3B567105315%3B571186496%3B571465797%3B573193634%3B574928208%3B581852411%3B582924369%3B586547013%3B590785283%3B598551031%3B604447975%3B604453786%3B605872780%3B609914788%3B610253930%3B622380143%3B629483834%3B630574516%3B634440693%3B643310215%3B650649639%3B651494188%3B651924756%3B652461742%3B656962313%3B661918945%3B661998339%3B664159349%3B664324800%3B665830535%3B666119895%3B669042740%3B669487771%3B671818043%3B674163610%3B679703080%3B679896885%3B680091381%3B685109708%3B692204469%3B692352393%3B693576505%3B693928505%3B699488218%3B701739208%3B703143157%3B703300211%3B704147427%3B704352069%3B706628328%3B707212201%3B710302950%3B717950556%3B718247850%3B718919677%3B719122851%3B719327908%3B721589487%3B723456023%3B724313618%3B730053692%3B730213552%3B730772971%3B731063017%3B732807275%3B734153852%3B741814595%3B744021831%3B747573207%3B757513263%3B761994028%3B764849512%3B768416191%3B778049378%3B787958910%3B796159721%3B809959098%3B813053535%3B824155955%3B828627299%3B829283957%3B839005746%3B840872511%3B843484646%3B844689451%3B845054629%3B848234476%3B852765233%3B1000576937%3B1004304958%3B1008047991%3B1015473777%3B1017743491%3B1020619341%3B1020634556%3B1022678407%3B1023262389%3B1027385681%3B1027928195%3B1035250530%3B1035682459%3B1052977966%3B1053597503%3B1053698030%3B1053732688%3B1055380176%3B1056809377%3B1058587382%3B1060608663%3B1063400405%3B1070533909%3B1071767114%3B1071960619%3B1083176585%3B1086503710%3B1087028651%3B1098303369%3B1102757875%3B1109486064%3B1114196865%3B1116084571%3B1118574365%3B1121530847%3B1131787898%3B1131844471%3B1139894525%3B1143831375%3B1152161894%3B1158181182%3B1162073970%3B1164303696%3B1169673373%3B1172231885%3B1173130469%3B1183431189%3B1196034452%3B1198250613%3B1198982056%3B1206280376%3B1234018094%3B1239793079%3B1247503402%3B1257937702%3B1259277492%3B1261616635%3B1261878519%3B1262185875%3B1262445302%3B1262860806%3B1279208856%3B1283016457%3B1286422809%3B1287099230%3B1290921212%3B1293397307%3B1295115242%3B1299756204%3B1301842759%3B1301894500%3B1303104957%3B1308482429%3B1309907587%3B1315291027%3B1316680391%3B1320260852%3B1320939128%3B1330608951%3B1331857064%3B1336024766%3B1338002765%3B1338164896%3B1340123828%3B1341027466%3B1364370718%3B1372797126%3B1374493422%3B1379764795%3B1385935903%3B1386666983%3B1388194610%3B1394134884%3B1396579405%3B1397262202%3B1413500064%3B1416054712%3B1416278524%3B1419585672%3B1423117714%3B1425565353%3B1425724864%3B1428687609%3B1431258197%3B1432462685%3B1443510711%3B1451771231%3B1461466444%3B1466047615%3B1468071638%3B1469403287%3B1469967488%3B1472741560%3B1476414128%3B1481087253%3B1496143753%3B1498819694%3B1527222617%3B1528932311%3B1543536576%3B1546344746%3B1550691037%3B1561694613%3B1566066725%3B1566708962%3B1574505614%3B1579232428%3B1586513572%3B1589304316%3B1594473889%3B1594832634%3B1603731306%3B1604595394%3B1609852565%3B1613941523%3B1616079433%3B1640532852%3B1647785629%3B1647818850%3B1652657165%3B1652692462%3B1654162596%3B1665113660%3B1671666642%3B1673079987%3B1674924955%3B1684864348%3B1686953559%3B1696398912%3B1707110056%3B1723821296%3B1725502898%3B1734295296%3B1738519132%3B1766790244%3B1770533548%3B1790070772%3B1806074453%3B1811074491%3B1816601020%3B1825935025%3B1831602865%3B1833842830%3B1840845367%3B1845821959%3B1848783678%3B100000016449937%3B100000019867713%3B100000026477681%3B100000029297898%3B100000035209870%3B100000045910860%3B100000052168531%3B100000064429697%3B100000075943822%3B100000081384508%3B100000081625223%3B100000101565871%3B100000113205523%3B100000114450197%3B100000117235726%3B100000120592690%3B100000136610153%3B100000138631630%3B100000145338135%3B100000167190323%3B100000174560028%3B100000177933371%3B100000188166652%3B100000190981209%3B100000221733230%3B100000250747936%3B100000260544345%3B100000299484386%3B100000309574997%3B100000340054929%3B100000341466714%3B100000387311865%3B100000398435130%3B100000407242074%3B100000417740773%3B100000419520193%3B100000421796247%3B100000452581205%3B100000462064468%3B100000508106410%3B100000517696868%3B100000524301515%3B100000576789119%3B100000583457306%3B100000597264802%3B100000607913679%3B100000676513831%3B100000692781035%3B100000733864194%3B100000774336252%3B100000830847624%3B100000847736287%3B100000853715298%3B100000856045877%3B100001001895214%3B100001029519786%3B100001093536134%3B100001157343775%3B100001220835854%3B100001230609233%3B100001381358286%3B100001405028095%3B100001440467822%3B100001481025895%3B100001494495513%3B100001530916865%3B100001534554460%3B100001544507518%3B100001558721850%3B100001614355131%3B100001645000011%3B100001986328418%3B100002073097971%3B100002352400400%3B100002383841123%3B100002637920147%3B100002795361523%3B100002852964846%3B100002897727776%3B100003116356881%3B100003319830274%3B100003333136152%3B100003713341268&requesterMail=something%40hotmail.com
and I got the result with "400 (Bad Request)", is something wrong with my request? or shall I divide my request into several request?
ids was containing list of id with semicolon separator.
If your query string exceeds maxQueryStringLength ASP.NET will return a 400 (Bad Request) status.
This setting defaults to 2048, you can increase it, however, browsers also restrict the query string / url length, for example IE9 allows a maximum of 2083 characters.
If you can change the code, instead of using $.getJSON with a long query string I would send the data via a POST request using $.post with the dataType: json:
$.post(yourUrl, yourData, function (data) {
// data has the same value as if you used $.getJSON
}, 'json');

Categories

Resources