views:

31

answers:

3

Hello. I'm diving into ASP.NET MVC 2 and I'm trying to understand how it handles different request formats. In Ruby on Rails, you specify in the controller which response format to return based on the request...

respond_to do |format|
  format.html #action.html.erb
  format.xml { render :xml => @employees.to_xml(:root => "employees") }

In ASP.NET MVC 2...

  1. How do you specify the request format?
  2. How do you respond to that request with the requested format?

Thanks so much in advance! I apologize if this question is strange, I'm very new to the .NET world.

+1  A: 

Haven't seen anything like that in asp.mvc. As far as I was concerned I was filtering my requests based on [Post] or [Get] attributes. The only thing I know is that you can allow Json requests to be processed by setting JsonRequestBehavior to JsonRequestBehavior.AllowGet option.

Nazar Gargol
You can also check whether a request is made using the built-in AJAX libraries.
bzlm
+1  A: 

As others have said there is no built-in support for formats in ASP.NET MVC. I've seen people add a QueryString parameters "format" to indicate the kind of format required (XML vs HTML) but you still need to manually code the response type on your controller.

There is support for JSON in MVC, but again, you need to manually evaluate whether the request wants JSON provide it. In the case of JSON is typical to see something like this:

if (Request.IsAjaxRequest())
{
    return Json(viewModel, JsonRequestBehavior.AllowGet);
}
Hector
+1  A: 

Download MVCContrib and here you go. You can also take a look at Simply Restful Routing.

Darin Dimitrov