ASP.NET WebApi vs MVC?

asp.net Mvcasp.net Web-Api

asp.net Mvc Problem Overview


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 ?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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.

Solution 5 - asp.net Mvc

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.

Solution 6 - asp.net Mvc

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

Solution 7 - asp.net Mvc

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

Solution 8 - asp.net Mvc

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

Solution 9 - asp.net Mvc

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.

Solution 10 - asp.net Mvc

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..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHugo ZapataView Question on Stackoverflow
Solution 1 - asp.net MvcYusubovView Answer on Stackoverflow
Solution 2 - asp.net MvcPaolo del MundoView Answer on Stackoverflow
Solution 3 - asp.net MvccuriousBoyView Answer on Stackoverflow
Solution 4 - asp.net MvcMysterionView Answer on Stackoverflow
Solution 5 - asp.net MvcWhat-About-BobView Answer on Stackoverflow
Solution 6 - asp.net MvcBehrouzMoslemView Answer on Stackoverflow
Solution 7 - asp.net MvcCoderView Answer on Stackoverflow
Solution 8 - asp.net MvcMurugesan NatarajView Answer on Stackoverflow
Solution 9 - asp.net MvcNikhil FegadeView Answer on Stackoverflow
Solution 10 - asp.net MvcEitan RevachView Answer on Stackoverflow