ASP.NET Core

Exploring web api in asp.net core

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.

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

Disqus Comments Loading...
Share
Published by
Karthik Chintala
Tags: web api

Recent Posts

2 Good Tools To Test gRPC Server Applications

In this post, we’ll see how to test gRPC Server applications using different clients. And… Read More

11 months ago

Exploring gRPC project in ASP.NET Core

In this post, we'll create a new gRPC project in ASP.NET Core and see what's… Read More

1 year ago

Run dotnet core projects without opening visual studio

In this blog post, we’ll see how to run dotnet core projects without opening visual… Read More

1 year ago

Programmatically evaluating policies in ASP.NET Core

Programmatically evaluating policies is useful when we want to provide access or hide some data… Read More

1 year ago

Multiple authorization handlers for the same requirement in ASP.NET Core

We saw how we could set up policy-based authorization in our previous article. In this… Read More

1 year ago

Policy-Based Authorization in ASP.NET Core

What is policy-based authorization and how to set up policy-based authorization with handlers and policies… Read More

1 year ago

This website uses cookies.