Creating MVC views that are not HTML

This is a general question about MVC as a template, but in this case I am using ASP.NET MVC.

I need to create an application whose output is an HTTP access XML stream (content type text / xml).

I can do this using traditional ASP.NET using the Generic Handler object.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/xml";
    context.Response.Write(someXmlText);
}

      

Is it possible to create ASP.NET MVC View that will achieve the same result?

Is this the right kind of MVC view?

+1


a source to share


2 answers


You can use MvcContrib XmlResult . This works the same as your example above. You don't need to use a view to render XML.



Essentially - you have an action on a controller that returns XML.

+3


a source


you can return it directly without views, you just need to specify the content type in the response:

for example, you can specify a course of action like this:



XElement GetElements(param1,param2...)
{
    XElement elements = new XElement("elements",
                                from c in element
                                select new XElement("element",
                                                     new XElement("Id",c.Id),
                                                     new XElement("Name",c.Name)
                                                    ));


    this.ControllerContext.HttpContext.Response.ContentType = "application/xml";
    return elements;
}

      

+1


a source







All Articles