views:

357

answers:

1

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?

+3  A: 

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.

tvanfosson
Many thanks! I knew I was confusing something so obvious!
Todd Brooks