Question

I have an ActionResult that returns XML for an embedded device. The relevant code is:

return Content(someString, "text/xml", Encoding.UTF8);

Even though UTF-8 is specified, the resulting XML is:

<?xml version="1.0" encoding="utf-16"?>

The ASP.NET MVC is compiled as AnyCPU and runs on a Windows 2008 server.

Why is it not returning UTF-8 encoded XML?

Was it helpful?

Solution

You are confusing the encoding of the HTTP response with the encoding of the XML contained in the response. When you serialize the XML you need to specify that it needs to be UTF-8 encoded. Setting the encoding on the ContentResult simply informs the browser on the other end how the response was encoded, it doesn't transform the XML from one encoding to another. If you look at the code for ContentResult, you'll see that it it simply does a Response.Write( Content ) -- after setting the Response headers with the encoding and content types you specify.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top