Creating API in ASP.NET Core
https://pixabay.com/en/children-win-success-video-game-593313/

Creating API in ASP.NET Core

Introduction

This article of the series “Web API with ASP.NET Core” will focus on creating Web API with ASP.NET Core. In the last article of the series, we learned about ASP.NET Core basics and how to set up an empty solution and play with request pipeline and middleware. In this article, we’ll discuss less theory and try to create an API. We’ll make use of ASP.NET Core MVC Middleware to build the API. We’ll cover in details on how to return resources, data and how to talk to API via HTTP request in this article. We can use the same source code as we got at the completion of last article of the series.

Middleware

To build the API, we’ll add a middleware in HTTP request pipeline, and we can do that by adding the middleware in Configure() method of Startup class. To do this, we need to add the service to the container. In the earlier version of .NET, we used to build services via Web API in ASP.NET. For creating an application with user interface that is used by the end-user, we used ASP.NET MVC. When we talk about ASP.NET Core, we do not have a separate Web API framework now.

The functionalities of MVC and Web API are now combined in one framework i.e. ASP.NET Core MVC. Its purpose is to build web applications that use APIs and MVC approach. To create an API, we need to configure the middleware and add that to the request pipeline.

MVC Pattern

MVC stands for Model View Controller. MVC is basically an architectural design pattern to define user interfaces. It encourages loose coupling and separation of concern with better testability. It consists of three parts.

The Model takes care of the business logic. In some cases, when the MVC Pattern is only used at the top level of application’s architecture, in that case model doesn’t contain the logic, but a component of application layer such as business layer handles it. In some implementations, use of View model is made to retrieve and store data. The View is the UI layer, it takes responsibility to show the data in a well-structured format with a rich UI for e.g. in the form of HTML. The controller takes care of the communication between view and the model, and also makes decision based on user inputs. In terms of dependencies of these components, the controller and the view depend upon model and the controller to some extent also depends upon the view. So a controller makes decision to choose the view to be displayed to user based on user input and provide the data from the model to the view in case needed. But when we talk about building an API, we should see how this structure maps to our need. So in the terminology of MVC, View is basically the data that we get as response that is it represents our resources or data in the form of JSON.

In our case, we expect that a request will come from any other user application having a user interface that just wants to access our service; when request comes, an action on our controller will be invoked, the controller will send the input data, and a model is returned to the View. View in our case would not be an aspx, razor, or html page but will represent result data in JSON format.

ASP.NET Core MVC Middleware

Now, we’ll try to add ASP.NET Core MVC middleware to our application. In the startup class, we can add the middle ware, but to proceed with that implementation, first add the AspNetCore.MVC nuGet package to the application as the service is defined in the external dependency named Microsoft.AspNetCore.Mvc NuGet package. Read More...



To view or add a comment, sign in

More articles by Akhil Mittal

Others also viewed

Explore content categories