views:

474

answers:

2

When requesting http://someserver.com/user/btyndall I'd like to return HTML When requesting http://someserver.com/user/btyndall?format=xml I'd like to return XML representation of my model

I've downloaded MvcContrib. (I can't believe XmlResult is not a part of the core framework)

What is the proper way to handle the request in the controller. With JSON you have a JsonResult and Json(). I see a XmlResult but not an Xml() method

I could use a little guidance. What I have so far (which is nada):

public ActionResult Details(int id)
{
  return View();
}

UPDATE:
see all comments

A: 

What about just returning two different views?

public ActionResult Details(int id, string format) {
  if (!String.IsNullOrEmpty(format) && format == "xml") {
    return View("MyView_Xml");
  }
  else {
    return View("MyView_Html");
  }
}

Then create two views. MyView_Xml:

<%@ Page Inherits="System.Web.Mvc.ViewPage<Customer>" ContentType="text/xml">
<?xml version="1.0" encoding="utf-8" ?>
<customer>
  <first_name><%= Model.FirstName %></first_name>
  <last_name><%= Model.FirstName %></last_name>
</customer>

and MyView_Html

<%@ Page Inherits="System.Web.Mvc.ViewPage<Customer>">
<html>
  <body>
    <div><label>First Name:</label><%= Mode.FirstName %></div>
    <div><label>Last Name:</label><%= Mode.LastName %></div>
  </body>
</html>
David G
I see where this would help me serialize out to XML, but what if I wanted to except XML for the Create() method? Seems like I'm handwriting more code here.
tyndall
Use [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(XDocument xml) { } to accept an XML post
David G
+1  A: 

This post shows a nice way of achieving what you are looking for.

Darin Dimitrov
Trying this method now. Thanks. Will post back in a little while.
tyndall