Difference Between MVC and Web API


This article explains the differences between ASP.NET Model View Controller (MVC) and the ASP.NET Web API and will also explain when to use the Web API with the MVC.

Overview of MVC

Model View Controller (MVC) divides an application into the three parts, Model, View and Controller. ASP.NET has many options for creating Web applications using the ASP.NET Web forms. MVC framework Combines the ASP.NET features such as Master pages, Membership based authentication. MVC exists in the "System.Web.MVC" assembly.
The components that are included by the MVC:
  • Models: Models are the objects used to retrieve and store the model state in the database.  Let's see an example. There is an "item" object that fetches the data from the database and performs an operation and then stores the updated data into the database. If an application only reads the dataset and sends it to the view then the application does not have any associated class and physical layer model.
  • View: View components show the User Interface (UI) of the applications that is created by the data model. For example the view of the Items table shows the drop down list and textboxes that depend on the current state of the "item"  object.
  • Controllers: In MVC, controllers are also called the components. These components manage the user interaction and chooses a view for displaying the UI. The main work of the controller is that it manages the query string values and transfers these values to the models.
The Models retrieve the information and store the updated information in the database. Views are used for only displaying the information, and the controllers are used for managing and responding to the user inputs and their interaction.

Overview of the Web API

The ASP.NET Web API allows for displaying the data in various formats, such as XML and JSON. It is a framework that uses the HTTP services and makes it easy to provide the response to the client request. The response depends on the request of the clients. The web API builds the HTTP services and manages the request using the HTTP protocols. The Web API is an open source and it can be hosted in the application or on the IIS .The request may be GET, POST, DELETE or PUT. We can say that the Web API:
  • Is an HTTP service.
  • Is designed for reaching the broad range of clients.
  • Uses the HTTP application.

Difference between MVC and Web API

There are many differences between MVC and Web API, including:
  • We can use the MVC for developing  the Web application that replies as both data and views but the Web API is used for generating the HTTP services that replies only as data.
  • In the Web API the request performs tracing with the actions depending on the HTTP services but the MVC request performs tracing with the action name.
  • The Web API returns the data in various formats, such as JSON, XML and other format based on the accept header of the request. But the MVC returns the data in the JSON format by using JSONResult.
  • The Web API supports content negotiation, self hosting. All these are not supported by the MVC.
  • The Web API includes the various features of the MVC, such as routing, model binding but these features are different and are defined in the "System.Web.Http" assembly. And the MVC features are defined in the " System.Web.Mvc" assembly.
  • The Web API helps the creation of RESTful services over the .Net Framework but the MVC does not support.
When we combined the MVC with Web API:
  • When we do the self hosting on the application, in it we combine both the MVC controller and the API in a single project and it helps for managing the AJAX requests and returns the response in XML, JSON and other Formats.
  • We combined the MVC and Web API for enabling the authorization for an application. In it we create two filters, one for the Web API and another for MVC.
This figure describes the combination of ASP.NET MVC and ASP.NET Web API.

Similar Articles