Using Web API only on ASP.NET Core – Without MVC specific stuff

Web API vs MVC

MVC and Web API have many things in common. Things like filters, attributes, controllers.  Since MVC and Web API share so many things with ASP.NET Core we got these two unified into one framework.

ASP.NET Core is the new ASP.NET framework. And you can use it to build web applications. If you want to use a framework like MVC and get a lot of “opinionated” features then you would use ASP.NET Core MVC.

ASP.NET Core MVC is a framework that builds on top of ASP.NET Core and it includes both MVC and Web API.

If you want to use MVC (and Web API) you will add Microsoft.AspNetCore.Mvc package. However, that adds a lot of MVC specific stuff. Here is the list of packages that Microsoft.AspNetCore.Mvc includes

 

As you can see from the list this includes Razor Pages, Tag Helpers, View Features. It’s quite obvious that for pure REST you don’t want these included.

If we check the code for the internal AddMvc extension we can see these things clearly:

 

Now you are thinking; for Web API I probably need JSON Formatters and Cors. However, you would also need MvcCore. This includes stuff like routing, attributes, filters, result executors, model binders, controllers etc.

Basically, stuff required for both MVC and Web API.

AddMvcCore is another built-in extension method that returns IMvcCoreBuilder.

So, to include Web API only you will install Microsoft.AspNetCore.Mvc.Core package. However, if you want CORS features with your Web API (and you probably want) you need to install Microsoft.AspNetCore.Mvc.Cors package. You probably want formatters for JSON input and output. That’s what Microsoft.AspNetCore.Mvc.Formatters.Json package adds.

You could also add Microsoft.AspNetCore.Mvc.ApiExplorer. Although, that’s if you want to use it. That’s if you wanna use the tool like Swagger or anything that does describe API endpoints in an application.

So, three packages that you usually require for Web API:

Now, my ConfigureServices would look a bit different.

Here is how Startup class looks:

Now we have core things that are required for both Web API and MVC. However, we don’t have MVC specific features.

There is one more thing that you need to be aware. When you are doing controllers in your application, the Controller parent class will not be available. You need to use ControllerBase class. And that is a base class for an MVC controller without view support.

Regular Controller class uses ControllerBase and adds some View specific features. Here is an overview of Controller class:

So, we don’t really want that in our Web API project, so we will use ControllerBase class.

Here is an example of the controller:

 

 

Code Sample

You can find the code sample on GitHub – AspNetCoreWebAPIOnly.

Ibrahim Šuta

Software Engineer, Mentor and Consultant interested and specialising in ASP.NET Core, C#, JavaScript, Angular, React.js.
He can help with MeteorJS, Node.js, ASP.NET, Windows Forms and Web Forms as well.