153

With ASP.NET MVC controllers you can expose your data in different formats. AspNetWebAPI is designed explicitly for creating API's but I can easily do that with MVC controllers, is not clear to me in what cases it would be better than traditional MVC controllers. I'm interested in scenarios where the benefits of WebApi are obvious and it would be worthy to add another complexity layer to my applications.

Question: what are the advantages and/or disadvantages of using asp.net WebApi in respect to MVC ?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Hugo Zapata
  • 4,314
  • 5
  • 29
  • 32
  • 1
    possible duplicate of [Difference between ApiController and Controller in ASP.NET MVC](http://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc) – digawp Jun 04 '15 at 14:04
  • 13
    It's funny to me how random these "vs" questions either get closed immediately or climb to this level of up-votes. – cbmeeks Sep 09 '16 at 18:46
  • 15
    Grrr. I hate those "closed as too broard" banners. So many times I find great questions like this. And more often then not they also have great answers. So what if it's too broad. The experts are right here and not on wikipedia or some other forum. So let them do their thing and get out of the way. – nharrer Apr 28 '18 at 13:49

10 Answers10

52

WebApi allows to create services that can be exposed over HTTP rather than through a formal service such as WCF or SOAP. Another difference is in the way how WebApi uses Http protocol and makes it truly First class Http citizen.

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

A relevant link of comparison, discussions & tutorials:

enter image description here

Yusubov
  • 5,815
  • 9
  • 32
  • 69
  • 21
    Disagree that the other post is similar. The other post is actually about WCF vs WebAPI. – Shane Courtrille May 16 '14 at 20:08
  • Technically speaking it says WCF Web API, however post mentions on the second line that it is merged into Asp.net web api now and supports self hosting. I have updated link anyway with more recent one. Hope that will help. – Yusubov Sep 19 '15 at 02:47
  • Updated the references. – Yusubov Jan 03 '17 at 19:45
24

WebAPI spits out OData, so you get all of the advantages of using OData. For example, with WebAPI you get:

  • Query options such as $filter, $top, $orderby, etc.
    • With traditional MVC controllers you need to implement these yourself.
  • Standardization of the format
    • There are OData clients that will understand the underlying format of your RESTful API.
Paolo del Mundo
  • 2,121
  • 13
  • 18
  • 6
    Sort of. I CAN support oData, but only if you decorate the action with the 'queryable' attribute, at least as of the RC. – EBarr Jul 06 '12 at 14:34
21

Asp.Net Web API VS Asp.Net MVC enter image description here 1. Asp.Net MVC is used to create web applications that returns both views and data but Asp.Net Web API is used to create full blown HTTP services with easy and simple way that returns only data not view.

2. Web API helps to build REST-ful services over the .NET Framework and it also support content-negotiation(it's about deciding the best response format data that could be acceptable by the client. it could be JSON,XML,ATOM or other formatted data), self hosting which are not in MVC.

3. Web API also takes care of returning data in particular format like JSON,XML or any other based upon the Accept header in the request and you don't worry about that. MVC only return data in JSON format using JsonResult.

4. In Web API the request are mapped to the actions based on HTTP verbs but in MVC it is mapped to actions name.

5. Asp.Net Web API is new framework and part of the core ASP.NET framework. The model binding, filters, routing and others MVC features exist in Web API are different from MVC and exists in the new System.Web.Http assembly. In MVC, these featues exist with in System.Web.Mvc. Hence Web API can also be used with Asp.Net and as a stand alone service layer.

6. You can mix Web API and MVC controller in a single project to handle advanced AJAX requests which may return data in JSON, XML or any others format and building a full blown HTTP service. Typically, this will be called Web API self hosting.

7. When you have mixed MVC and Web API controller and you want to implement the authorization then you have to create two filters one for MVC and another for Web API since boths are different.

8. Moreover, Web API is light weight architecture and except the web application it can also be used with smart phone apps.

Original source is here

curiousBoy
  • 6,334
  • 5
  • 48
  • 56
15

Similarities

  1. both inherits from ihttphandler for the asyncrequest so basically apicontroller or mvc controller both are the wrapper around the web.http

Differences:

  1. MVC controller is very heavy if you could go through its definition you can see how many interfaces and the base code it has used, web API is lighter controller and distinguish request by its passed parameters ( yes we can change it too!)

  2. MVC controller has too many features like it return views, action result, javascript result, etc but in web API has either JSON or XML.

  3. API is for implementing Restful(GET, POST, PUT, DELETE, OPTIONS) services which can be independently hosted anywhere without the depending upon views, MVC controller cant support that as it tightly integrated with the views.

Monis
  • 165
  • 2
  • 12
Mysterion
  • 384
  • 4
  • 9
  • 3
    Its worth noting that you can still accomplish API like behavior with MVC using mvc action results like JSONResult, etc. However as you pointed out the MVC controller is heavier. Also the way that Web API controllers are implemented are more conducive to making an API than MVC. – Grizzly Peak Software Jul 12 '17 at 23:47
14

At some point you might want to forget ASP.NET MVC all together. If you are a .NET developer but you want to build a Single-Page application (using Angular for instance) you'll want the benefits of a RESTful service (WebAPI) without all of the unnecessary bloat that comes with ASP.NET MVC.

What-About-Bob
  • 649
  • 5
  • 13
13

This image seems to show the differences and similarities in the way the matter. I hope that helps is interesting for me.

enter image description here

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34
7

ASP.NET MVC And ASP.NET Web api both of them are using for different purpose

ASP.NET

ASP.NET offers three frameworks for creating web applications: Web Forms, ASP.NET MVC, and ASP.NET Web Pages. All three frameworks are stable and mature, and you can create great web applications with any of them. No matter what framework you choose, you will get all the benefits and features of ASP.NET everywhere.

Each framework targets a different development style. The one you choose depends on a combination of your programming assets (knowledge, skills, and development experience), the type of application you’re creating, and the development approach you’re comfortable with. All three frameworks will be supported, updated, and improved in future releases of ASP.NET.

MVC

ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, TDD-friendly development for creating sophisticated applications that use the latest web standards.

Web API

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

you can read more form here http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-Web-API.html

Coder
  • 406
  • 9
  • 22
6

ASP.NET MVC is focused on making output of HTML easy. ASP.NET Web API is focused on making output of raw data easy.

In the WebForms world, ASP.NET MVC would be equivalent to .aspx pages and ASP.NET Web API would be .asmx

3

Asp.Net MVC is used to create web applications that returns both views and data but Asp.Net Web API is used to create full blown HTTP services with easy and simple way that returns only data not view.

Web API helps to build REST-ful services over the .NET Framework and it also support content-negotiation(it's about deciding the best response format data that could be acceptable by the client. it could be JSON,XML, self hosting which are not in MVC.

Web API also takes care of returning data in particular format like JSON,XML or any other based upon the Accept header in the request and you don't worry about that. MVC only return data in JSON format using JsonResult.

1

Usually, WebAPI used for data services where MVC can generate more types of outputs.

WebAPI for sure simplifies the way we can create data services. It's clean and easy for this purpose. MVC come with some more tools around.

MVC can generate any output WebAPI can output. Generating outputs from templates can be easily achieved in MVC. I can't find a reason to do so in WebAPI. PHP developers and old ASP programmer might know this attitude from the past, where you can build HTML files involved with C# code inside.

WebAPI- DATA MVC - DATA, UI/HTML, XHTML, Files, Templates etc..