Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC and text/xml content type

I want to return a View() from an action, and the resulting response should have a content type of text/xml instead of the default text/html.

I have tried the following, with no success:

Response.ContentType = "text/xml"; 
return View();

I know that you can specify the content type by returning ContentResult, but that doesn't render my View.

I'm hoping I don't need to render the view to a string then use return Content(), so I'm probably overlooking some easy way.

like image 675
andreialecu Avatar asked Jun 09 '09 18:06

andreialecu


2 Answers

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" 
    ContentType="text/xml" %>
like image 162
eu-ge-ne Avatar answered Nov 03 '22 12:11

eu-ge-ne


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
}; 
like image 43
Alex Avatar answered Nov 03 '22 13:11

Alex