Introduction
In this article, I am going to highlight the differences between ASP.NET MVC Controllers and Web API 2.0 Controllers, illustrating that with samples of code. However, I will emphasize more on Web API 2.0 as it is the latest evolution in Microsoft web services toolkit.
Advantages of Web API over MVC Controllers
API Controllers decouples code from serialization of results:
Every method in Web API will return data (JSON) without serialization.
However, in order to return JSON Data in MVC controllers, we will set the returned Action Result type to JsonResult and call the Json method on our object to ensure it is packaged in JSON.
- public JsonResult GetBooks()
- {
- var books = _repository.GetBooks();
- return Json(books, JsonRequestBehavior.AllowGet);
- }
Components called formatters serializes the data returned to the client. They are automatically selected based on the content of the Accept header of the incoming request. You can either use built- in formatter such as: JSON and Xml or replace them by modifying the configuration.
Content negotiation plays a role in simplifying the development of method that might return the same raw data in a variety of formats, most typically XML and JSON.
Hosting:
Web API controllers can be hosted outside ASP.NET Runtime Stack and IIS Web server (things WCF was able to do) and are mainly used to build restful services hosted by specific organization and can be consumed by any device (android, IOS, web or windows application).
MVC controllers typically rely on MVC framework and are built with the view in mind (mainly returning HTML to the browser).
Now, we will dig deeper into code and implementation and check the key differences:
Controllers
MVC controllers uses the base class Controller which is defined in System.Web.Mvc while API uses ApiController which is defined in System.Web.Http
- public class BooksController: Controller
- {
- public ActionResult Details(int id)
- {
- var book = _repository.GetBook(id);
- if (book == null) returnnewHttpNotFoundResult();
- return View(book);
- }
- }
- public class Books Controller: ApiController
- {
- public Book GetById(int id)
- {
- var book = _repository.GetBook(id);
- if (book == null) thrownewHttpResponseException(HttpStatusCode.NotFound);
- return book;
- }
- }
For actions that don’t match with one of those verbs, the default verb supported will be “POST”. Thus, we have to decorate all the actions that don’t meet with this naming conventions by one of the following attributes:
- HttpGet
- HttpPut
- HttpPost
- HttpDelete
MVC Controllers by default dispatch actions by name. A specific action name in a controller will directly map to the URL.
The two routes (routes configuration are explained later in this article) to the actions in the previous code will be the following:
- /api/books/{id}will route to ASP.NET Web API,where /api will occupy the URI space by default for all actions, followed by controller name and the parameter expected.
- /books/details/{id} will route to ASP.NET MVC (as ControllerName/ActionName/{parameter}).
Action Return Values
MVC controller methods returns objects of types of ActionResult that can produce variety of results. These are some of the predefined action results in ASP.NET MVC:
| Action Result | Behavior |
| ContentResult | Sends raw data to the browser. It serialize any content it receives. |
| FileContentResult | Sends the content of the file to the browser. |
| FileStreamResult | Sends the content of the file to the browser (which is represented using Stream object). |
| HttpNotFoundResult | Sends HTTP 404 response code (Resource was not found). |
| HttpUnauthorizedResult | Sends HTTP 401 response code (Unauthorized request). |
| JavaScriptResult | Sends JavaScript content to the browser. |
| JsonResult | Sends JSON result to the browser. |
| ViewResult | Send HTML content to the browser and represents a page view. |
| PartialResult | Sends HTML content to the browser that represents a part of the whole page view. |
On the other hand, Web API 2.0 can return:
- HttpResponseMessage:
Raw objects (when passing domain models) are converted automatically to the suitable format (JSON or XML) using Content Negotiation which is a Web API feature. However, it is difficult to return different representation of success and errors in the response and this will make unit testing more difficult.
- publicHttpResponseMessage Get()
- {
- //Retrieve a list of books from the DB
- IEnumerable < Book > books = _repository.GetBooks();
- //Write the list to the response body.
- HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, books);
- return response;
- }
- IHttpActionResult:
Introduced in Web API 2.0 and will simplify unit testing controllers by returning a value type of IHttpActionResult. The ApiController includes many methods that directly return those action Results which sounds like MVC controller.
- publicIHttpActionResult Get()
- {
- // Get a list of books from the DB.
- IEnumerable < Book > books = _repository.GetBooks();
- if (books == null)
- {
- return NotFound();
- }
- // Returns an Ok Negotiated ContentResult
- return Ok(books);
- }
The following are the methods in the ApiController that return action results:
| Method | Behavior |
| BadRequest | Returns an HTTP 400 (“Bad Request”) |
| Conflict | Returns an HTTP 409 (“Conflict”) |
| Content | Returns Content (which is automatically negotiated or specified by the developer as media type formatter or content type) |
| Created | Returns an HTTP 201 |
| InternalServerError | Returns an HTTP 500 (“Internal Server Error”) |
| Json | Returns an HTTP 200 (“OK”) and provides the content formatted in JSON |
| NotFound | Returns an HTTP 404 (“Not Found”) |
| Ok | Returns an HTTP 200 (“OK”) |
| Redirect | Returns an HTTP 302 (“Found”) |
| ResponseMessage | Returns the provided HttpResponseMessage |
| StatusCode | Returns a response with provided HTTP status code and an empty response body |
| Unauthorized | Returns an HTTP 401 (“Unauthorized) |
Configuration
Web API 2.0 is designed not to have static global variables (as in traditional ASP.NET Applications where they share application configuration in Global.asax). Routes are defined in the WebApiConfig static class and puts its configurations into the HttpConfiguration object and its Route property.
- publicstaticclassWebApiConfig
- {
- //Routing configuration for Web API
- public staticvoid Register(HttpConfiguration config)
- {
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new
- {
- id = RouteParameter.Optional
- }
- );
- }
- }
- public class RouteConfig
- {
- public staticvoid RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new
- {
- controller = "Home", action = "Index", id =
- UrlParameter.Optional
- }
- );
- }
- }
Also, you can see how RouteConfig can add the routes to the Route table for MVC.
- protected void Application_Start()
- {
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- //configuring the WebApi by calling Register method and passing the
- paramerter
- WebApiConfig.Register(GlobalConfiguration.Configuration);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles)
- AuthConfig.RegisterAuth();
- }
While MVC controllers can be consumed as services and do anything with additional coding, its main purpose is to build web HTML applications with some Ajax functions returning JSON data. On the other hand, Web API 2.0 is dedicated and specialized with transferring data and will be the optimal choice and the suitable development model that makes it easy to build RESTful services interface to different clients running on different platforms.

keyur soniPosted Jul 25, 2018, 12:39 AM
Very helpful article.
Jeff onesPosted Jul 3, 2017, 10:36 AM
You assume they are the same technologies done differently. That is incorrect. MVC is an architectural design, where as WebAPi is communications design. WebAPI works just fine for a browser or native client to communicate with the MVC backend. Even the pieces of an MVC-implemented system can be spread between web servers and application servers.
Sumesh SukumaranPosted Mar 15, 2017, 12:47 PM
Its good very useful
Humayun Kabir MamunPosted Jan 10, 2016, 9:31 AM
Nice...
Ankit BansalPosted Jan 10, 2016, 2:47 AM
nice one..keep sharing..
Hussein SalmanPosted Jan 8, 2016, 1:08 PM
My conclusion also agrees with your comment
Hussein SalmanPosted Jan 8, 2016, 1:04 PM
@Maruthi, I am just comparing the points that they both provide from implementation point of view. Also, i am stating the advantages of Web API over MVC controllers since i am emphasizing on Web API in this Article.
Maruthi PalllamalliPosted Jan 8, 2016, 12:29 PM
subjected one. WebApi was purely defined HTTP oriented, but MVC not like that its has more features. You can give Webapi behaviour to mvc application but cant give mvc features to web api.
Hussein SalmanPosted Jan 6, 2016, 12:50 PM
Thanks dear friends
Santhakumar MunuswamyPosted Jan 5, 2016, 11:37 PM
Welcome
Santhakumar MunuswamyPosted Jan 5, 2016, 11:37 PM
Good Start
Kumaresh RajalingamPosted Jan 5, 2016, 10:55 AM
Good one sir
Muhammad Aqib ShehzadPosted Jan 5, 2016, 7:42 AM
Nice one Dear
Sabyasachi MishraPosted Jan 4, 2016, 7:25 AM
Good one
Hussein SalmanPosted Jan 4, 2016, 6:12 AM
Hello Akash, JSON is the default MIME type that Web API services return, you don't have to right additional code to do the serialization (it is automatic). This depends on the content negotiation, for example it you want to return XML as a response, change the "Content-Type" to "application/xml", it will automatically serialize and return data as XML rather than JSON. So, "JSON" and "XML" are handled out of the box. If another formats are required, we have to write our custom serializers.
Akash VarshneyPosted Jan 4, 2016, 5:32 AM
Hi Hussein Salman' . Can you please provide a link where i can find that WebApi to dont need serialization as you have mentioned in the first line. "Every method in Web API will return data (JSON) without serialization." i don't thik this is correct .. Thanks for the contribution !! ..
Raja TPosted Jan 3, 2016, 11:52 PM
Nice, Thanks for sharing
Gowtham KPosted Jan 3, 2016, 10:05 AM
Good One, Thanks for sharing
Ankur MistryPosted Jan 3, 2016, 8:40 AM
Nice share
Rupesh KahanePosted Jan 3, 2016, 7:27 AM
Good one
Nilesh JadavPosted Jan 3, 2016, 6:46 AM
Good Share !
Sridhar SharmaPosted Jan 3, 2016, 6:13 AM
Nice Share