Reputation: 67223
This web page has an ASP.NET MVC code sample that includes the lines:
[Route("sitemap.xml")]
public ActionResult SitemapXml()
{
var sitemapNodes = GetSitemapNodes(this.Url);
string xml = GetSitemapDocument(sitemapNodes);
return this.Content(xml, ContentType.Xml, Encoding.UTF8);
}
But if I attempt to insert this code into an MVC project, Intellisense gives me the error:
The name 'ContentType' does not exist in the current context
So I added a using
statement for System.Net.Mime, but then I get the error:
'ContentType' does not contain a definition for 'Xml'
So, okay, I give up. Where is the definition for ContentType.Xml
?
Upvotes: 2
Views: 6827
Reputation: 129717
The second parameter for the Content
method of the MVC Controller
class expects a MIME media type string, such as "application/json"
or "image/jpeg"
.
If you're looking for predefined constants to use for this parameter, there is a limited set defined in these classes:
For example, you could use System.Net.Mime.MediaTypeNames.Text.Xml
, which equates to "text/xml"
.
However, the list of constants defined in the above classes is nowhere near complete. Notably missing are "application/json"
, "application/xml"
and "image/png"
to name just a few. Also, there are no classes for the audio or video mime types. If you need any of these, you will need to define your own constants. (For reference, the complete list of official media types can be found here.)
As for the definition of the ContentType.Xml
constant referenced in your question, it looks to be defined in a separate Framework project from the same author which the MVC 5 and MVC 6 sample templates seem to depend on. The source code for both the Framework and the Templates can be found here on GitHub. The ContentType
class can be found here.
Upvotes: 3
Reputation: 12768
You can find some built-in constants, though not attached to ContentType, in
They're limited and based on the mime-type hierarchy ("text/xml" is found in Text.Xml) so not quite what you're looking for. They have the further limitation that "Xml" is only defined in Text and most people recommend you use "application/xml" rather than "text/xml".
In your example above, it looks like the author has a helpful git project for this. You can add it to your project using NuGet. Search for BoilerPlate.Web.MVC6 (or 5). You can see the class he's using here. It has a lot of useful constants for ContentType.
Upvotes: 2
Reputation: 793
Use MVCContrib's XmlResult Action.
Below is the code for class
public class XmlResult : ActionResult
{
private object objectToSerialize;
/// <summary>
/// Initializes a new instance of the <see cref="XmlResult"/> class.
/// </summary>
/// <param name="objectToSerialize">The object to serialize to XML.</param>
public XmlResult(object objectToSerialize)
{
this.objectToSerialize = objectToSerialize;
}
/// <summary>
/// Gets the object to be serialized to XML.
/// </summary>
public object ObjectToSerialize
{
get { return this.objectToSerialize; }
}
/// <summary>
/// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
/// </summary>
/// <param name="context">The controller context for the current request.</param>
public override void ExecuteResult(ControllerContext context)
{
if (this.objectToSerialize != null)
{
context.HttpContext.Response.Clear();
var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
context.HttpContext.Response.ContentType = "text/xml";
xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
}
}
}
Upvotes: 0
Reputation: 173
You should change the code to render xml.
public ActionResult Index()
{
string xml =
"<?xml version='1.0' encoding='UTF-8'?>
<urlset>
<url>www.google.com</url>
</urlset>";
return Content(xml, "text/xml");
}
Upvotes: -1