MVC vs. Web API: Which ASP.NET technology should you use?


In this article, we will be talking about the differences between ASP.NET Model View Controller (MVC) and the ASP.NET Web API, and which you should be using for different requirements. Before going deeper, let’s get a short intro of what MVC actually is.

MVC or Model-View- Controller is what divides an application actually.

  • Model – These are objects that fetch the model state in the database, i.e. it retrieves the data from the database, does a particular operation and stores the data into the database.
  • View – Here, the developers can ‘view’ the UI or User Interface of the applications created by data model.
  • Controllers – Controllers or Components manage the user interaction, manage the query string values and transfers the values into models.

You find MVC in the “System.Web.MVC ” assembly. The MVC framework makes use of the ASP.NET features like Membership based authentication, Master pages, etc. You can try the various options in ASP.NET for making web applications with ASP.NET web forms.

Through ASP.NET Web API, you can display forms of data like XML and JSON. This framework is open-source, uses HTTP service, and responding to client requests is a piece of cake. The requests themselves (for example, Get, Post, Delete, Put etc.) are managed using HTTP protocols. ASP.NET Web API can be hosted on the IIS or in the application.

Beginners usually get confused on the choice of ASPNET technology to be used. Here are a couple of instances where you can use each of these technologies.

1) The process of Exposing functionality

ASP.NET MVC controllers would be a good choice (1) if you are looking to expose a functionality within a single application or (2) if it is to be used as a generic functionality on any application. You can save time this way because you don’t have to create a new API to expose functionality each time. Normally, a controller is attached to a particular web application and it exposes functionalities to be quickly consumed through Ajax.

If you are looking to create a full-fledged REST service, not attached to a single application, then you can use Web API, because it delivers an elegant and neat solution.

If the functionality is UI or View centric, like loading HTML fragments or creating AJAX driven pages, then ASP.NET MVC are a better choice. Web API would be a good choice when you are creating a standalone RESTful service.

If the functionality is data-centric, choose Web API servers. Example, CRUD operations. In normal MVC applications, just the controllers are sufficient to return both data and views.

2) The data formats to deal with

Controllers return either ActionResult or JsonResult, meaning the controller output could be either HTML markup or JSON formatted data. If one of these data formats are enough then you can use action methods to expose functionalities. However, when multiple data formats are involved, then Web API would be the better choice. This is because this technology can automatically decide the web format by looking at the Accept header. In the MVC controller, you will have to specifically specify the data format to write action methods.

Web API can be used for generating HTTP services that replies data alone, but MVC would be suitable for developing web applications that replies as both, views and data.

Web API looks at Accept Header of the request who returning the data in various formats, so it can return in various formats, like XML, JSON etc. But for MVC, the data returning is only in the JSON format using JSONResult.

3) When you combine MVC and Web API

Developers can enjoy a couple of benefits when they combine both MVC controller and API.

(1) You can manage the AJAX requests and return the response in multiple formats when you combine both.

(2) You can create two different filters for authorization for an application The requests are mapped to the actions on HTTP verbs, but it is mapped to the actions name in MVC. You can use API as a standalone service layer, and it can also be used with ASP.NET.

The model binding, routing, filters are all different in Web API, and exists in the System.web.http assembly. These features exist in the System.web.MVC in MVC framework.

4) Whether you need self-hosting


As you know, controller, a part of the ASP.NET MVC applications needs IIS as the hosting environment. However, API is a service framework and you can self host it. This makes it lightweight, because you can avoid the overheads of IIS. This is a great choice when your app has to be released in a number of platforms – desktop, web applications, console applications etc.

Closing thoughts

You can choose Web API if you are generating a full-blown HTTP service like Flickr, Amazon, Delicious, etc. It is also a good selection if you need content negotiation. Content negotiation is a process of returning content in a format mentioned by Accept Header. However, you may not need it for all your projects, because sending data as JSON and XML is mostly what’s required. With Web API, you have the option of sending the content in a variety of formats, including images and files.

WebAPI is perhaps a better framework when you want to exhibit your data and services to various devices. And it is open source, and hence the perfect platform for creating RESTful services over DotNet framework. And you don’t have to perform any additional configuration settings for each device separately. These days people prefer to use more of mobile apps when compared to desktop applications, and so it becomes very important to develop both type of apps.

MVC, on the other hand, provides clean Separation of Concerns, allows full control over rendered HTML, allows easy integration with Javascript frameworks, and enables Test Driven Development.

Interesting Articles:
Why is Web API better than MVC
Brief points on MVC vs API

Picture Source: Flickr.com/ Dennis/ Yamashita


The author: Reema Oamkumar is engaged as a thought leader at www.Software-Developer-India.com which is a part of the YUHIRO Group. YUHIRO is a German-Indian enterprise which provides programmers to IT companies, agencies and IT departments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.