5

I have two ASP.NET MVC web applications deployed under IIS8 (let me call them sender and receiver web applications). I am calling an action method inside the receiver web application from an action method inside sender.

Now inside the sender, I have the following action method that will upload a string to an 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 am encoding the string using UTF-8 before uploading it, since I will be passing unicode characters such as £ , ¬ , etc...

On the receiver web application, the receiving action method is as follows:

public List<CRUDOutput> CreateResource(Resource resourceinfo)

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

However, it seems on the receiving action method the resourceinfo received the correct decoded values. So it seems like ASP.NET MVC will handle the decoding automatically somewhere.

First question:

Can anyone tell me how ASP.NET MVC handles the decoding inside the receiving action 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");

Yet, it seems that this does not have any effect in my case. When i remove the above line of code, nothing changes. Can anyone tell me if defining the content type header will have any effect in my case?

Last question:

If I do not explicitly define the UTF-8 encoding using wc.Encoding = System.Text.Encoding.UTF8; will WebClient() use a default encoding?

Will Ray
  • 10,621
  • 3
  • 46
  • 61
John John
  • 1
  • 72
  • 238
  • 501
  • Normally, you do not need to encode data. Look like you post a string to CreateResource action method, but it is expecting a model. What is Resource? – Win Aug 02 '16 at 21:12
  • @Win but if i do not encode using UTF8 then i will not be able to pass non-ASCII characters such as £ or ¬ ,, that why i chose to encode the data to be able to pass non-ASCII characters... other wise if i pass charecter such as £ from sender ,, it will be received as ? inside the receiver action method... for the Resource it is model class which i have created on both the sender and the receiver web applications..beside this i am sending a json string from sender which represents the Resource model class (the json string which is being sent is a serialize representation of the model class)... – John John Aug 02 '16 at 21:24

1 Answers1

4

First: Where/what does the decoding of an Http Request?

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

The web api pipeline poster provides a very good depiction of when this happens, and what does it.

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

If there is a media type formatter (highlighted in red above) that matches the MIME header in the HTTP request, that media type formatter will read the message. As described in the model binding resources, the media type formatters can be configured via the HttpConfiguration.Formatters property at Application Start, usually in Startup.cs or Global.Asax.cs. I'll bet that if you debug, and inspect Config.Formatters, you'll find a JsonFormatter that is configured for UTF-8.

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

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

Second: Why doesn't specifying the charset seem to have an effect?

Have you sent any higher level characters that would have made a difference, or tried setting a different charset other than UTF8 for the header? W3Schools has a good table "Differences Between Charsets" try using that to test the differences of your charset headers 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 charset?

I'm pretty sure, but don't have any solid resource for this, that UTF-8 is the de-facto standard these days for encoding/decoding strings. I can't really recall the last time I have seen anything other than UTF-8 except for the last time I programmed in C.

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

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

P.S. I feel obligated to mention Joel's great post on this topic.

Community
  • 1
  • 1
Sam
  • 1,052
  • 11
  • 17