开发者

Why use ASP.NET MVC 2 for REST services? Why not WCF?

开发者 https://www.devze.com 2022-12-25 01:27 出处:网络
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.

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.


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.


There's now another option, Web API. ScottGu has a brief introduction in his blog and there's an interesting blog post discussing creating APIs using the Web API vs controllers here.


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.


I can't talk for the MVC toolkit, but personally I find WCF a pain to work with. It does have uses, but it's quite a heavy framework and can be quite awkward to use at times.

If you want to go down the REST route, OpenRasta is the best .Net solution currently around. Especially in terms of compliance.

You can also forge the ASP MVC framework to be RESTful, but that requires a fair bit of work (compared to OpenRasta which works out of the box).


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));
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消