Unified MVC & Web Api Controllers - C# ASP.Net Core

Unified MVC & Web Api Controllers - C# ASP.Net Core

ASP.Net MVC 4 Controllers

Recently I worked on a project that was written in ASP.Net MVC 4. The tech stack itself was split between Web Api and MVC itself, in separate projects with different code bases. The lack of unified controllers compared to what ASP.Net Core now offers felt like going back in time…

…So given that, let’s take a very brief look at unified MVC & Web Api controllers in C# ASP.Net Core!

Prior To Unification

Before ASP.Net Core we essentially had two separate underlying assemblies at the core of our web stack (MVC vs. Web Api), since both implementations were based on the MVC model and therefore required the use of a Controller class. MVC was based on System.Web.Mvc and would produce one type of controller, specifically for the standard ASP.Net MVC design which involved returning cshtml views. Whereas Web Api was based on System.Web.Http and would produce an ApiController which allowed the return of JSON in a RESTful manner.

To add some context, here’s a simple example of using both MVC and Web Api when implementing your code base using MVC4. Starting with MVC

C# ASP.Net MVC 4 - MVC View Controller Action

C# ASP.Net MVC 4 - MVC View Controller Action

…and then Web Api, of course separately.

C# ASP.Net MVC 4 - Web Api Endpoint Controller Action

C# ASP.Net MVC 4 - Web Api Endpoint Controller Action

There were numerous issues with this design including the copious amounts of configuration required to get simple routing setup, not least the fact that it prevented easy grouping of related actions.

Enter “ASP.Net Core 2”…

With the advent of ASP.Net Core both MVC and Web Api controller implementations now inherit from the same Controller base class, which is part of the Microsoft.AspNetCore.Mvc assembly. The unification doesn’t stop here either - The actions have been combined too, along with many other components such as the routing and filters that we are used to taking advantage of.

Here is a glorious ASP.Net Core 2 unified controller -

C# ASP.Net Core - Unified MVC & Web Api Controller

C# ASP.Net Core - Unified MVC & Web Api Controller

Enjoy!