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?
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:
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());
}
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With