How / Where asp.net mvc handles decoding and action method parameters

I have two ASP.NET MVC web applications deployed in IIS8 (let me call them sender and receiver web applications). I call the action method inside the recipient's web application from the action method inside the sender.

Now, inside the sender, I have the following action method that will output string to the external action method on the receiver:

 using (WebClient wc = new WebClient()) { var data = JsonConvert.SerializeObject(resource); string url = "https://receiver/CreateResource?AUTHTOKEN=" + pmtoken; Uri uri = new Uri(url); wc.Encoding = System.Text.Encoding.UTF8; wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8"); wc.Headers.Add("Authorization", token); output = wc.UploadString(uri, data) } 

I encode string with UTF-8 before loading it, since I will pass Unicode characters like £ , ¬ etc.

In the recipient’s web application, the receive action method is as follows:

 public List<CRUDOutput> CreateResource(Resource resourceinfo) 

In the beginning, I thought my approach would not work well. Since I am sending encoded data (using wc.Encoding = System.Text.Encoding.UTF8; ) from the sender to the receiver's action method, and I am not doing any decoding in the receiver's action method.

However, by the receive method, resourceinfo received the correct decoded values. So it looks like ASP.NET MVC will handle decoding automatically somewhere.

First question:

Can someone tell me how ASP.NET MVC handles decoding inside the receiving method?

Second question:

Inside my WebClient() method, I define the following to specify the content type header:

  wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8"); 

However, this does not seem to have any effect in my case. When I delete the above line of code, nothing changes. Can someone tell me if the definition of the header of the content type will have any effect in my case?

Last question:

Unless I explicitly define the encoding of UTF-8 with wc.Encoding = System.Text.Encoding.UTF8; will WebClient() use the default encoding?

+5
source share
1 answer

First: where / what does the Http request decrypt?

In ASP.NET, the process of accepting an HTTP request and turning it into C # POCO is called Model Binding .

The api website poster gives a very good idea of ​​when this happens and what it does.

Here is an excerpt from there to show the area that I think you are looking for: Model Binding Diagram

If the HTTP request has a media type formatter (highlighted in red) that matches the MIME header in the HTTP request, this media type format will read this message. As described in the model binding resources , media file format formats can be configured using HttpConfiguration.Formatters in Application Start, usually in Startup.cs or Global.Asax.cs. I bet if you debug and check Config.Formatters, you will find JsonFormatter configured for UTF-8.

The tutorial has a code snippet in the section "Adding a Media File to the Web API Pipeline" for setting custom formatting.

 public static void ConfigureApis(HttpConfiguration config) { config.Formatters.Add(new ProductCsvFormatter()); } 

Second: Why doesn't it indicate that the encoding seems to have an effect?

Did you send any higher-level characters that could be affected, or tried to set a different encoding other than UTF8 for the header? W3Schools has a good table, “Differences between Encodings,” try using this to check for differences in the headers of your encodings and the resulting decoded values.

I think the real answer to this question is the answer to your third question ...

Third: what is the default encoding?

I'm sure, but I don't have a solid resource for this, that UTF-8 is the de facto standard these days for encoding / decoding strings. I can’t remember the last time I saw anything other than UTF-8, except the last time I programmed it in C.

However, in .NET you can verify the encoding of WebClient using the WebClient.Encoding property . It says that: A Encoding that is used to encode strings. The default value of this property is the encoding returned by Default. A Encoding that is used to encode strings. The default value of this property is the encoding returned by Default. Where 'Default': Encoding.Default . I would definitely not be surprised if you debug your program and find that it is UTF-8.

A good idea would be to play around with this setting and use Fiddler to check the headers received.

PS I feel obligated to mention Joel's excellent publication in this section .

+4
source

All Articles