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 respons开发者_开发技巧e 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...
- How do you specify the request format?
- 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.
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);
}
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.
Download MVCContrib and here you go. You can also take a look at Simply Restful Routing.
精彩评论