While developing a web application using MVC, many developers get confused when to use the ASP.NET API. An MVC framework can return JSON data by using JsonResult and can also handle simple AJAX requests.
The previous article, Web API and WCF the Most poppuler Frameworks of Microsoftt, compared WCF with Web API and WCF REST with Web Service. In addition, it also described when to use a Web API over other services. In this article, you will learn when to use Web API with MVC.
Asp.Net API VS Asp.Net MVC
- With Asp.Net MVC, one can create web applications that return both views and data.But, Asp.Net Web API allows creating full-blown HTTP services with an ease that returns only data, not views.
- Web API helps to build REST-ful services over the .NET Framework. It also supports content-negotiation and self-hosting that MVC doesn’t support. Content-negotiation refers to deciding the best response format data acceptable by the client. It could be JSON, XML, ATOM or other formatted data.
- ASP.NET API also takes care of returning data in a particular format like JSON, XML or any other. It decides the return format using the Accept header in the request and the developer needs not worry about that. On the contrary, MVC only returns data in JSON format using JsonResult.
- The Web API maps the request to the actions based on HTTP verbs. On the other hand, MVC maps requests based on the action names.
- Net Web API is a new framework and a part of the core ASP.NET framework. MVC features like model binding, filters, routing, and others existing in Web API differ from MVC. These features exist in the new System.Web.Httpassembly. In MVC, these features exist within System.Web.Mvc. Hence, ASP.NET can use Web API as a stand-alone service layer.
- Also, you can mix the Web API & MVC controller in a single project to handle advanced AJAX requests. The returned data will be of JSON, XML or any other format. You can also use it to build a full-blown HTTP service. Typically, this defines the term Web API self-hosting.
- In case, you have mixed MVC and ASP.NET API controller to implement authorization; you need to create two filters. You need one filter for MVC and another for Web API since both are different.
- Moreover, Web API is a lightweight architecture. Apart from the web application, it also finds use in smartphone apps
Is this article helps you? You are most welcome to add the 9th point in the list. Feel free to provide your comments and concerns in the below comment section.