views:

140

answers:

2

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

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

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

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

Can I create an ASP.NET MVC View that achieves the same result?

Is this an appropriate use of an MVC View?

+3  A: 

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

In essence - you have an action on a controller that returns the XML.

Matt Hinze
if you look at the mvc source you'll find that is quite simple build you own actionresult for serving what you want (i just did an ImageThumbnailResult)
Andrea Balducci
@Richard yes, .
Matt Hinze
you have an action on a *controller* that returns the XML.
Matt Hinze
To go a little farther, you can make a custom filtering attribute to pick the method based on the content-type in the request. That way you don't need method "method" and method "methodXML" or similar nonsense.
EvilRyry
+1  A: 

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

for example you can specify action method 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;
}
Marko