views:

1726

answers:

3

With ASP.NET MVC, it is common to have AJAX code(e.g. jQuery) to invoke web service from server to without page refreshing. It's natural to make web service RESTful. It seems that there are two ways to go. First, the ASP.NET MVC URI's are RESTful, it is very easy to make some Controller/Action to act as web service method. Second, WCF can be RESTful since version 3.5.

So, what is pros and cons of these two ways?

Requests to ASP.NET MVC will go through ASP.NET Pipeline. Does this make it slower than WCF?

+8  A: 

If you're already using ASP.Net MVC for the rest of the website, I suppose it makes sense to use the same framework for the AJAX calls as well.

With regard to the ASP.Net pipeline, I assume you're worried about the whole Page Lifecycle thing. The page lifecycle is only executed if you use Views with the WebFormViewEngine. The framework provides JsonResult for easy JSON serialization of action results, which completely bypasses the ASP.Net page lifecycle. Similar classes are available for XML, RSS, etc.

Jacob
+2  A: 

You have to differentiate AJAX calls from REST APIs in a sense that AJAX calls are made in the context of your application and can rely on the application context for things like session, authentication etc. REST API however is a set of APIs you provide for the world to consume - as it can rely on your application for things like authentication it brings a whole new complexity for securing calls from your clients etc.

If you just need your application's JS to talk with the server then using MVC controllers is the easiest straightforward solution.

If you want those APIs separated from your web site's MVC code (for deployment purposes etc.) or if you need a REST API for others to use to call your app - WCF.

Eran Kampf
+1  A: 

On my blog http://shouldersofgiants.co.uk/Blog/ I've put together a series looking at using ASP.Net MVC to provide a RESTful web service if that helps.