Exploring web api in asp.net core

exploring apis

With the asp.net core modularity, the configuration and setup work for the asp.net core projects is more when compared to building apps with the .net framework.

In this post, we’ll explore how to build a web API and also look at the configuration options that we need to build a web API in asp.net core.

I have installed ASP.NET CORE 2.2 for this project. Make sure you install the .NET Core 2.2 SDK if you are running the sample project.

File >> New Project

Open Visual Studio and from the menu select File >> New >> Project. This should open a New Project wizard.

Select .Net Core from the left menu and select ASP.NET Core Web Application. That should bring another wizard with a good number of project templates. Choose API from that.

Once done with that you should see a brand new project.

Exploring the files created

Solution explorer should look like this.

ASP.NET CORE 2.2 solution explorer for API

The solution doesn’t have much of the boilerplate that you’d normally expect with the normal .net framework API with the default project.

Looking at the ValuesController

ValuesController is the file created by the API project template.

Unlike the normal .net framework API controllers which will be derived from ApiController class, the ValuesController in ASP.NET Core 2.2 is derived from ControllerBase class.

public class ValuesController : ControllerBase

The ControllerBase abstract class has the same things what the old ApiController class in the .net framework has. So, things like the ModelState, HttpContext, Url information and several methods which returns HttpResponses from the API’s.

The ApiController annotation

Apart from deriving the API from the ControllerBase class, we have our values controller API decorated with [ApiController].

The ApiController annotation is not mandatory on the controllers. Your API still works if you don’t decorate with ApiController annotation. However, having the ApiController annotation has its advantages.

They derive this attribute from the ASP.NET MVC ControllerAttribute so using this on a class will make it available for the controller discovery process. Besides having implemented ControllerAttribute class, it also implements IApiBehaviorMetadata, which will provide some API-centric features.

ApiController annotation, when coupled with ControllerBase, will provide REST specific behavior for the controllers.

Automatic model state validation

Model state validations will automatically trigger an HTTP 400 response. So, writing the model state condition in every controller action isn’t necessary.

if (!ModelState.IsValid)
{
    return BadRequest(ModelState);
}

We can customize the error message of the above 400 response by using InvalidModelStateResponseFactory

Conclusion

If you are new to ASP.NET core consider reading the documentation from MSDN so you can get a good grip on the setup and configuration things in the startup.cs file.

Overall, the API seems to be the same old ASP.NET MVC Web API except for those configuration things in the Program.cs and Startup.cs files.

References

Tags: