2

I plan to write web app which will work mostly on client side (AJAX) and RESTfully communicate with server using JSON and have another client which will use XML (or eventually JSON too). This app will have client authentication but is rather simple and small.

Which is better choice for this RESTfully web service: WCF or ASP.NET MVC?

ccellar
  • 10,326
  • 2
  • 38
  • 56
Pol
  • 5,064
  • 4
  • 32
  • 51

2 Answers2

5

At this point, if you are starting a project now I would head down the WCF WebAPI route. MVC provides the basic infrastructure for building a REST API but doesn't have built in support for things like content type negotiation.

The WebAPI is intended to provide first class support for REST style services for the .NET framework

However, the Web API hasn't yet been released (although I'm pretty sure it will be this year) so you will be developing on beta code - oh and its also available on Nuget

btw - the CHM file has some good stuff in it to get you started

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23
  • Is this content type negotiation useful for PUT operations? If not, what else WCF WebAPI can offer what ASP.NET MVC can't? – Pol Jan 23 '12 at 10:07
  • You can do full GET, PUT, POST, DELETE with MVC. For most applications I'm not sure what the WCF WebAPI offers over MVC (if the MVC is done as shown in Omar Al Zabir's project). – Tom Chantler Jan 23 '12 at 10:35
  • @Dommer - I understand that MVC can of course handle GET, PUT, POST, DELETE. I was just saying that content type negotiation mentioned in this answer as benefit is not as important since it it probably (if I am not wrong) is useful only for GET. In my case I want to support both JSON and XML in GET and PUT. – Pol Jan 23 '12 at 11:14
  • I don't see the benefit of the WCF content-type negotiation either (but I may just need educating of course). In the CodeProject article I linked the return type is worked out based on the type of the request. There's an example where you can send XML or JSON to the same URL and it will respond with XML or JSON autoamtically. It looks pretty good! – Tom Chantler Jan 23 '12 at 11:27
  • 1
    Content type negotiation is important for GET as you say but can be used for others (so for example I have to send you XML but I would prefer a different content type back in general - say a JQuery client). However content type negotiation is one example of REST style idioms that WebAPI supports that you would have to hand roll using MVC: JSONP and OData support are also there out of the box that you would have to hand roll with MVC. I'm certainly not saying you can;t build RESTful APIs with MVC - just why not use the tool that specifically designed for the job – Richard Blewett Jan 23 '12 at 22:04
  • @Richard: Fair point about using the right tool. If you need to do both it's way easier just to tweak the MVC app. I guess each solution has it's place, hence the first sentence in my answer. Web API not yet being released would put me off a bit, but I'll look into it. Cheers for the heads up :-) – Tom Chantler Jan 24 '12 at 13:06
3

The right answer is probably: It depends.

If you're already familiar with one of the technologies, then maybe you should use that one, but I'd rather use ASP.NET MVC.

I am now going to mention two articles by Omar Al Zabir

If you want to go down the ASP.NET MVC route then read this: http://www.codeproject.com/Articles/233572/Build-truly-RESTful-API-and-website-using-same-ASP

If you're going to use WCF then read this about improving scalability: http://www.codeproject.com/Articles/234085/Fixing-WCF-to-build-highly-scalable-async-REST-API

The cool thing about using MVC is that you can write a RESTful app and expose a RESTful API all with the same code.

EDIT: Just to clarify, the MVC RESTful project DOES support GET, PUT, POST, DELETE, so it really is truly RESTful. See the documentation here: http://code.msdn.microsoft.com/Build-truly-RESTful-API-194a6253

specifically this bit:

All these URLs support GET, POST, PUT, DELETE. Users can browse to these URLs and get html page rendered. Client apps can make AJAX calls to these URLs to perform CRUD on these. Thus making a truly RESTful API and website.

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • I've read the MVC article, thanks for link. However, it uses MVC 2. Is this actual in MVC 3? – Pol Jan 24 '12 at 13:02