Hi I'm trying to return a view that is xml, meaning the content type will be "text/xml", and the view is using ASP.NET MVC razor. Another post ASP.NET MVC and text/xml content type showed how to do it with aspx view. How do I get the same done with razor?
Asked
Active
Viewed 1.9k times
4 Answers
33
I found an example of an rss feed produced with a razor view here:
Basically you have to set the Response.ContentType
to "text/xml"
, and then you can just write your xml as if it was html.
You have to scroll down to see the actual code so I'll copy it here:
@{
var db = Database.OpenFile("Database.sdf");
var getRss = db.Query("SELECT TOP(5) * FROM Table" );
Response.ContentType = "text/xml";
}
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Website name</title>
<link>website link</link>
<description>News for website</description>
<dc:language>en-gb</dc:language>
<dc:creator>email</dc:creator>
<dc:rights>Copyright 2010</dc:rights>
<admin:generatorAgent rdf:resource="http://www.styledna.net/" />
@foreach (var row in getRss) {
<item>
<title>@row.title</title>
<link>@row.link</link>
<description> some html desc for the item </description>
</item>
}
</channel>
</rss>

fretje
- 8,322
- 2
- 49
- 61
-
8@CrazyDart: Hey, it's an example... and the question is about setting the ContentType, not about what else is done in the view. The OP asks for the razor equivalent of `<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" ContentType="text/xml" %>` which is `@Response.ContentType = "text/xml"`. – fretje May 09 '11 at 23:17
-
Im with yah, I looked at the example link. I am not a big fan of copy pasting garbage code. I will remove the -1, but you need to take that junk code out so some noob doesn't think thats how things are done in MVC. – CrazyDart May 09 '11 at 23:22
-
1@fretje, +1 for answering the OP's question and defining the XML in a Razor view. – Kirk Woll May 09 '11 at 23:22
-
1This actually works, with the razor view having the xml body. And as far as the Response.ContentType, it works either setting it here or in the action method. Thanks a lot everyone! The only thing is I could not add this line in the razor file, as it adds it for you with the contenttype set to xml. – Ray May 09 '11 at 23:25
-
1@CrazyDart: I don't know... for some applications I think this is a much nicer way for outputting xml than fiddling around with xmlwriters or xmlserializers and what not... – fretje May 09 '11 at 23:25
-
4@fretje +1 for an entirely correct answer to the question posed. The query/header setting code is obviously just there to keep the example simple to illustrate the point; not as an example of 'best practices'. – Andrew Barber May 09 '11 at 23:27
-
@CrazyDart: the example I posted in the other forum shows the Web Pages framework approach, not an MVC View. – Mike Brind Sep 01 '11 at 12:21
-
This is a bad idea. What if you have a "&" or a "<" in one of your rss items? It all blows up. – Mark Aug 30 '15 at 13:34
-
@Mark: Have you tried it? '@' in Razor should automatically take care of that. – fretje Sep 01 '15 at 11:06
-
Razor would only take care of it for things that it would need to take care of for HTML, since Razor thinks it's outputting HTML. You'd need a way to tell Razor it's outputting XML. – Mark Sep 01 '15 at 16:31
-
Note to anyone: Weirdly for me setting the content type sometimes works in the Action but sometimes doesn't..... the fix in my particular instance was to have it in Razor – Tito Feb 03 '16 at 17:04
8
If you prefer you can instead make the content type change from your view action, like so:
public ActionResult MyAction() {
Response.ContentType = "text/xml";
return View();
}

Luis Perez
- 27,650
- 10
- 79
- 80
-
Warning: for some reason setting it in the action worked on some actions but not on others in my case – Tito Feb 03 '16 at 17:05
-
@Titus Maybe the content type is overridden. Either by the CSHTML using the method defined above, or by the ActionResult you are returning. For example ContentResult can be configured to set the content type. I think the order the content type can be set and overridden is Action method, action result, and CSHTML. Though I think the last two can alternate depending on how that ActionResult is implemented. – Luis Perez Feb 03 '16 at 20:57
2
For anyone trying to do this is ASP.NET Core, you can find the Response as a property of the Context:
@{
Context.Response.ContentType = "text/xml";
}
<?xml version="1.0" encoding="UTF-8" ?>
<doc>
...
</doc>
Although I found setting the content type in the Action worked perfectly well (as suggested by @Luis above)

Darren
- 4,408
- 4
- 39
- 57
0
A couple things you need to be aware of.
- Make sure to set the layout to null or else your output will include the root layout.
- Content type needs to be text/xml
- Finally, you need to make sure you have no carriage return at the start of the document since XML parsers expect to see the root <?xml element at the beginning. This means your <?xml needs to start right after the closing @{}
Controller
[HttpGet]
public ViewResult SomeXML()
{
return View();
}
View (SomeXML.cshtml)
@{
Layout = null;
Response.ContentType = "text/xml";
}<?xml version="1.0" encoding="utf-8" ?>
<element>
...
</element>

Narbs
- 111
- 6