136

I am new to ASP.NET MVC and Web API and trying to get the basics. AFAIK, we have project templates in VS 2013, named as MVC, Web API and Both of them together.

I have gone through the tutorials and learned that we can make an API by using MVC alone as well as with Web API Template.

So, What are the differences between these, based on Architecture and Usage?

loop
  • 9,002
  • 10
  • 40
  • 76
  • Today, in ASP.NET Core, there's no longer any distinction between MVC and Web APIs. There's only ASP.NET MVC: https://learn.microsoft.com/en-us/dotnet/architecture/porting-existing-aspnet-apps/webapi-differences – volkit Dec 01 '21 at 13:37

4 Answers4

185

Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse. They are the same in most other respects. The main difference between the project types is that the MVC Application project type adds web specific things like default CSS, JavaScript files and other resources needed for a web site, which are not needed for an API.

MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.

Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.

UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated into the Controller class. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

Community
  • 1
  • 1
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
  • 1
    Thanks Elad i got it.Can you make en edit to your ans and add something about Asp.net Identity and Web Api 2 Just some how are they going to be used and benefit.just some words. – loop Mar 23 '14 at 10:56
71

My two cents...

  1. In ASP.Net MVC – the MVC’s Controller decides what should be the View - i.e., the controller decides what the user should “see” (based on the current scenario or context), when they make a request.
  2. In ASP.Net Web Forms, the ASPX pages decides what the user should “see” when they make a request.
  3. But in Web API, there is no control/power to any of the Web API’s features to decide what the user should “see” when they make a request.

Web API is NOT a technology tied up with websites only. It can be used for multiple purposes – not only websites. So it doesn't know the meaning of rendering

Further Reading

  1. Planning Web Solutions Today: Web Forms, ASP.NET MVC, Web API, and OWIN.
  2. WCF or ASP.NET Web APIs? My two cents on the subject
  3. The Next Generation of .NET – ASP.NET vNext
  4. Getting Started with ASP.NET MVC 6
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • I cannot tell from the compared codes why WebAPI is more readable. Also, it looks there is something flipped there, as it is the Web API the one expected to have an HTTP method attribute such as [HttpGet]. Check some WebAPI (and WebAPI 2) code [here](http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results) – Gobe Feb 13 '16 at 05:34
  • 2
    The accepted answer states "Basically, a Web API controller is an MVC controller". That's simply wrong. Your reply points out that the whole *POINT* of WebAPI is that it *ISN'T* MVC, it *DOESN'T* have to deal with all the baggage and overhead for dealing with "Views" and "rendering". ... and WebApi controllers don't even have to be tied up with web sites only. Very good reply - thank you! – FoggyDay Nov 17 '18 at 22:58
0

MVC controller derived from controller class. In Mvc you can returns views. Mvc achitecture uses to create an application. However Web apis are used to provide data to various application.

Web Api drives from Api controller and it doesn't return view.

Note: You can also create Web Api from MVC controller but you need to return result as JsonResult or other web api supported return types.

Akshay Kapoor
  • 185
  • 1
  • 3
0

In addition to answers already provided here, its worth noting any controller which inherits from ApiController and having an action with Http verb POST can only have one [FromBody] input parameter. If using a MVC controller (deriving from 'Controller') you can have many post input parameters.

barrypicker
  • 9,740
  • 11
  • 65
  • 79