4

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

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • I'll be posting something soon to the ASP.NET CodePlex site http://aspnet.codeplex.com/ which will address this scenario. Stay tuned. :) – Haacked Aug 14 '09 at 16:24
  • I need something for a prototype next week. What strategy should I use in the meantime, Phil? – BuddyJoe Aug 14 '09 at 16:51

2 Answers2

0

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 Glenn
  • 24,412
  • 19
  • 74
  • 94
  • 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. – BuddyJoe Aug 14 '09 at 16:49
  • Use [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(XDocument xml) { } to accept an XML post – David Glenn Aug 14 '09 at 18:20
0

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928