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?
ASP.NET requires less expertise than MVC and is much faster to develop web applications overall. Prior knowledge of HTML is not required. ASP.NET uses a more mature code-base and has a larger web control toolbox. It's more sensible to use ASP.NET if you are already relying on 3rd party UI controls.
You should use Web API over ASP.Net MVC if you would want your controller to return data in multiple formats like, JSON, XML, etc. Also, specifying the data format in Web API is simple and easy to configure. Web API also scores over ASP.Net MVC in its ability to be self-hosted (similar to WCF).
WEB API is a better choice for simpler, light weight services. WEB API can use any text format including XML and is faster than WCF. WEB API can be used to create full-blown REST Services. WEB API doesn't require any data contracts and doesn't require configurations to the level of WCF.
The main advantages of ASP.net MVC are: Enables the full control over the rendered HTML. Provides clean separation of concerns(SoC). Enables Test Driven Development (TDD).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With