Smooth Fight Between Web API And ASP.NET MVC API

Yesterday, I discussed with one of my friends and the topic was why choosing Web API for the service instead of ASP.NET MVC API.

It was very interesting topic for me. At the end of the topic, what I learned, I am going to explain one by one.

Web API: It is a part of ASP.NET Framework to build and deploy the REST services on HTTP protocol and accessible to wide range of devices.

ASP.NET MVC API: When I am talking about ASP.NET MVC API, It means, it is also an API but creating with ASP.NET MVC application. I mean to say both view and data in the same project.

When you develop your web application using the MVC, many developers get confused between Web API and ASP.NET MVC. They are confused about which one will be better and how.

View and Data

As we all know Web API is used to create the fully REST service which is exposed only on HTTP protocol, so only HTTP enabled client is able to access the Web API service. Web API only returns data not view. There is not any graphical user interface to represent the data. Client can get the data and present it on their view.

But if you create the API using the ASP.NET MVC then you will be able to create view as well as return data. It means ASP.NET MVC APIs return data and represent this data on their view.

So, if you are going to create API which will be going to return only data then you need to prefer Web API otherwise it is good to go with ASP.NET MVC API.

MVC Vs Web API

Content Negotiation

Web API supports one of the most important features in API world and that is Content Negotiation. Web API decides and return best response format data that is acceptable by client very easily. The data format can be XML, JSON, ATOM or any other format data. But with ASP.NET MVC API, if we want the data in JSON, you need to cast your data.

Web API also supports self hosting with any ASP.NET application. So, if you think that your client application is not in ASP.NET MVC then you should always prefer to create your API in Web API.

But if you use ASP.NET MVC to create API then you cannot use self hosting feature with this application.

ASP.NET Web API

  1. public class EmployeeController: ApiController  
  2. {  
  3.     // GET: /Api/Employees/  
  4.     public List < Employee > Get()  
  5.     {  
  6.         return EmployeeMaster.GetEmployees();  
  7.     }  
  8. }  
ASP.NET MVC API
  1. public class EmployeeController: Controller  
  2. {  
  3.     // GET: /Employees/  
  4.     [HttpGet]  
  5.     public ActionResult Index()  
  6.     {  
  7.         return Json(EmployeeMaster.GetEmployees(), JsonRequestBehavior.AllowGet);  
  8.     }  
  9. }  
Standalone Service Layer

As we all know that ASP.NET Web API is part of ASP.NET framework. But the features of the Web API like filters, routing, model binding, etc. are totally different from ASP.NET MVC API because of ASP.NET Web API in System.Web.Http namespace but ASP.NET MVC API resides in System.Web.MVC. So, we can say that Web API is standalone and used with any other application which supports HTTP protocol.

So, Web API creates a service layer for the service. You can use HttpClient to access the Web API inside the ASP.NET MVC controller.

SoC [Separation of Concerns]

As we know APIs should be single entity over the internet for more flexibility and better accessibility. Web API separates your API from your project which contains static file and html pages. If you create your API with ASP.NET MVC API then you need to settle View, Partial View, Html helpers with APIs. So, it makes your APIs more complex as compared to Web API.

So, if you are not isolating API from your project then it is not best practice to keep the API and front end in the same project.

MVC

Performance

If you create your API using the ASP.NET MVC API then you will pay the performance issue with your API. I have seen that standalone API which is hosted on console apps are nearly 40 or more than 40% faster as compared to API which is created using ASP.NET MVC.

Web API is very light weight as compared to ASP.NET MVC API. So, using Web API increases the performance of the application when getting the data.

Authorization

If you are going to implement the authorization filters in your Web API then you just need to create only one filter for Authorization.

But when you mix MVC and Web API in the same project, then it is being tough and you need to create authorization filters for both.

Request Mapped

As you know Web API uses some HTTP verbs for CRUD operation and it is auto mapped with these verbs. But ASP.NET MVC is mapped with their action.

Which one is best choice?

So, finally as per my opinion the best thing is that Web API is a good choice when you are going to create standalone fully REST Service, but it is your project requirement to return data and represent it on View in same application. Then you should go with ASP.NET MVC API.

One more reason to choose Web API is that it provides us high performance in low cost as compared to ASP.NET MVC API.

So, finally as per my opinion ASP.NET Web API is the best instead of ASP.NET MVC API. But I suggest you need to try and see which one is better and why. You can add any other points which I left. You are most welcomed.

 


Similar Articles