ASP.NET MVC and text/xml content type

You need to render the string. To return text/xml do the following:

return new ContentResult {
    ContentType = "text/xml",
    Content = UTF8.GetString(yourXmlString),
    ContentEncoding = System.Text.Encoding.UTF8
}; 

You need a view that doesn't override things and generate HTML, including its own context-type.

A custom view can directly render to Response.Write (see JsonResult in Reflector for a class that is very similar to what you would need).

To render XML without a intermediate string, save your XML to an XmlWriter created over Response.Output.


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" 
    ContentType="text/xml" %>

Users control (ASCX) doesn't accept ContentType="text/xml".

Solution:

public ActionResult xxx()
  {
     Response.ContentType = "text/xml";
     return View("xxx.ascx");
  }