views:

67

answers:

2

Hi guys, just a question that needs a quick answer,

I have a Action, lets say,

BlogPostController.List();

Which lists all the Posts in a hypothetical blog engine.

I want both a HTML output of this data and an XML output of this data.

Preferably I'd like to be able to address these purely by URL, for example:

http://MyHypotheticalBlogEngine.com/BlogPosts/List

http://MyHypotheticalBlogEngine.com/BlogPosts/List.xml

And then when I call View() in my Action method it'd pick either the .aspx view or the .xml view depending.

Is this something built in (I can't seem to find info on it as it is, but I can't think of good keywords to really search for it either) or is it a "find another way or roll your own way" jobby?

Cheers

+3  A: 

You need to specify an input parameter that can be blank for the default view but needs to be specified to achieve the other various forms that you might want to support. In the case of an RSS reader you might support RSS, ATOM, XML, etc. Choose the default and then add to your url the other format types.

domain.com/blogs/list/
domain.com/blogs/list/xml
domain.com/blogs/list/atom

etc.

Andrew Siemer
ah, ta :) Simple solution. Got too hung up on the extension side of it :P Didn't think about adding an extra member to the path
Sekhat
+3  A: 

For starting just add a parameter to your action:

public ActionResult List(string format)
{
    ...

    if(String.Compare("xml", format, true) == 0)
    {
        return View("ListInXml");
    }

    return View("List");
}

In your Views you can create urls to this action without modifying your RouteTable:

<!-- for HTML -->
<%= Url.Action("list", "blogpost") %>

<!-- for XML -->
<%= Url.Action("list", "blogpost", new { format = "xml" }) %>
eu-ge-ne