views:

2831

answers:

4

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.Content = "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.

A: 

Have you tried setting the response.content from the view's pre render method in the codebehind page? that's obviously assuming you're using the webform view engine

Joel Martinez
My view doesn't have codebehind.
legenden
so add one ;-) .
Joel Martinez
:-/ Don't add one
Charlino
+5  A: 
Alex
Well, I already knew about this (specified in the question), but I want to avoid it.
legenden
+23  A: 

?:

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

Hope this helps

eu-ge-ne
Thanks, you're a genius!
Luke Sampson
+1  A: 

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.

Richard