Tuesday, January 29, 2019

Web API vs Asp.Net MVC API

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.


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. 



ASP.NET Web API
public class EmployeeController: ApiController  
{  
    // GET: /Api/Employees/  
    public List < Employee > Get()  
    {  
        return EmployeeMaster.GetEmployees();  
    }  
}  
ASP.NET MVC API
public class EmployeeController: Controller  
{  
    // GET: /Employees/  
    [HttpGet]  
    public ActionResult Index()  
    {  
        return Json(EmployeeMaster.GetEmployees(), JsonRequestBehavior.AllowGet);  
    }  
}  
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. 


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.

No comments:

Post a Comment

JWT Token in WebAPI

using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Net; using Syst...