views:

1041

answers:

3

So I see that MVC 2 now supports [HttpPut] and [HttpDelete] as well as [HttpGet] and [HttpPost], making it possible to do a full RESTful Web service using it.

I've been using the REST toolkit for WCF for a while and find it fairly powerful, but I'd be interested to find out what (if any) advantages there are using the MVC 2 approach.

Links, war stories, or even pure hear-say are welcome.

+4  A: 

I'm pretty sure ASP.NET MVC has supported all the HTTP verbs since the beginning. At least the HttpVerb Enumeration has had them from the beginning. The only thing that's new in V2 is that they are attributes.

// V1
[AcceptVerbs( HttpVerbs.Delete )]

// V2
[HttpDelete]

Six of one, half a dozen of the other. As to whether you want to expose functionality through WCF or ASP.NET MVC, it would come down to how you think of your application.

  • If you think of it as a thick client app that just happens to be written in JavaScript and calls out to restful services for data (then formats it client side) then WCF would feel like a more correct solution (even though you could do it using either).

  • However if you think of your application as a server app that returns content in some form or another for consumption, then using a RESTful API for your actions would make more sense. Your actions would return fully formatted content that would be displayed in the browser without a need for further processing. You could return formatted content (HTML or otherwise) from a web service, but that would somehow feel wrong.

At least that kind of distinction makes sense in my head =). You may also be interested in Phil Haack's post on How a Method Becomes an Action.

R0MANARMY
+2  A: 

We use ASP.Net MVC 1.0 to create JSON services.

There are a couple of reasons for this:

  1. We were using ASP.Net MVC for the pages, so using it for the services as well reduces the number of technologies in the project.
  2. We found it very easy to use for returning an ActionResult with JSON formatted data

    public ActionResult GetData(string id)
    {
        if (string.IsNullOrEmpty(id))
        {
            throw new ArgumentNullException("id", "Searchvalue must be provided.");
        }
    
    
    
    // Where Provider.GetData returns  IEnumerable<Data>
    return Json(Provider.GetData(id));
    
    }
Shiraz Bhaiji
You could also just return JsonResult
moi_meme
+1  A: 

You should check an interesting Blog shouldersofgiants Who made a series of post on Creating RESTful WebService with ASP.Net MVC.

from Part 1 to Part 21 you might find some iteresting information. And he's not even done yet... good reading.

moi_meme