Getting Started With ASP.NET Web API .NET 5

ASP.NET Web API

These days, most applications are developed using web API because of numerous advantages. Web API gives developers or integration engineers options to communicate among various applications. Web API decreases the dependencies and makes them less coupled among the applications.

Web API is not a technology but a concept and can be built using any technology like .Net, Java, Node js, PHP, and so on. One does not need to know the backend technologies for web API and can easily implement it which opens broad utilization and concepts.

In this article, we will explore in detail web API, what it is and how to develop Web API in ASP.Net i.e. ASP.NET Web API. We will learn the following topics

  • What is API and Web API
  • HTTP methods
  • ASP.NET web API
  • Create an ASP.NET web API (.NET core 5)
  • Implement Swagger for Web API Testing
  • Testing ASP.NET Web API
  • Benefits of ASP.NET Web API

API and Web API

API, Application Programming Interface is a kind of interface with a set of functions for connecting applications. Normally, the API enables programmers to utilize specific features, services, access data of an application, perform logic, operations in databases, and so on.

API are simply methods, procedures, requests, subroutines, or endpoints.

Web API: Most of the APIs are generally web-based which can be accessed over the internet using an HTTP request. Web API is a kind of web development concept and can be developed using any technology. It can be consumed by any application regardless of any programming language. Programmers do not need to know the underlying technologies or programming language of Web API to implement or consume it in their solutions.

Google API, for instance, is widely implemented or integrated into several applications such as Google Maps, distance, language, cognitive services, etc. Those solutions consuming google API, do not need to know the underlying technologies or language but should know the endpoints URL, and parameters.

Similarly, Microsoft provides a wide range of web APIs for various services like cognitive services, Azure services which provide capabilities to integrate with any applications.

HTTP Methods

The Hypertext Transfer Protocol is an application protocol to exchange information or data over the internet on the world wide web. HTTP verbs are used to get or send data to the server.

HTTP verbs are listed below,

GET – this request/operation is used to retrieve data from the web service.

POST – this request/operation is used to add content, message, and create a new item of data on the web resource.

PUT – this request/operation modifies an item of data on the web service.

DELETE – this operation removes a resource on the web service.

PATCH – this operation modifies an item of data on the existing web resource by describing a set of instructions about how the item should be updated. Normally, it is not used in the sample application.

ASP.NET Web API

The ASP.NET Web API is a .NET framework for building or developing RESTFUL API with HTTP-based services which can be accessed via any application. This web API can communicate with a wide range of clients through browsers, mobile devices, and by using web services.

The clients of API communicate via HTTP requests to exchange information using JSON, XML, or some other data format. This is also called back for Single Page Applications, where all the logic, operations, and database communications are done through API.

Web APIs are considered as RESTful APIs if those are defined with,

  • HTTP requests with GET, POST, PUT, DELETE, and PATCH
  • A base URL
  • Used JSON, XML Media type for the data

Create ASP.NET Web API

Now, we will create asp.net web API with .NET 5 (core). We will use

  • Visual Studio 2019 (any version community/professional/Enterprise)
  • .Net 5

Open Visual Studio and create a new project.

Then, we will choose the project template ASP.NET Core Web API as portrayed below.

We need to give a project name and location as shown:

Next step, we will get the option to choose the target framework and will choose .NET 5.0 which is the current version. Besides, there are some other options as well.

Target Framework: .NET 5.0

Authentication Type: None

Configure for Https

Enable docker

Enable OpenAPI support. (This will enable swagger, which we discuss later in this article)

We will click on the Create button to create the ASP.NET Web API.

Now, our web API project is created. We will build and run the project to check Web API. The output of the project is as shown below.

We have successfully created an ASP.NET Web API. Since swagger is implemented, we can test the API as well.

Let’s explore more about this web API project.

The project is implemented with a swagger as portrayed.

We can implement swagger by installing the NuGet package Swashbuckle.AspNetCore.

Swagger

This is OpenAPI, API documentation which is a language-agnostic specification for describing REST APIs. The Swagger project was contributed to the OpenAPI Initiative in 2015 and has since been referred to as OpenAPI.

  • Swagger gives API documentation and reduces the time required to prepare it
  • Reduce the amount needed to connect decoupled services
  • Gives UI to test Web API

Web API Controller

API is defined under controller folder with APIController Attribute as shown:

In the controller class, we define API controller, Route, and HTTP methods as the shown sample.

This is how we define the API controller. This is a dummy or sample API, however, in the next article, I will do a complete CRUD operation in ASP.NET web API.

Testing ASP.NET Web API

Since we have implemented swagger, we can easily test our API. We can test this web API with Post Man but in this article, we will test it with swagger as shown.

We will use the above interface as a swagger, where I have highlighted some key areas.

  • API Endpoint route/URL
  • Try it out: To try or test API
  • Parameters: this will give the option to enter parameters if any
  • Response output sample
  • Schemas: All the necessary schemas.

This is how we can create and get started with ASP.NET web API.

Some benefits of ASP.NET Web API (.NET 5)

ASP.NET Web API gives complete options to build any type of modern Web API. We can use the same .NET framework or .net core, same coding patterns to develop the Web API with numerous options. We can implement various design patterns and architecture using the ASP.NET Web API.

Web pages and Services in the same project

This means we can build web API or services as well as web pages in the same project which gives advantages of reusing model classes, validation logics, etc. in both.

Authentication and Authorization

We can use built-in .net authentication and authorization in ASP.NET web API to secure them. Built-in authentication and authorization are industry-standard JSON Web Tokens (JWT). This option gives you the flexibility to control the policy-based authorization and access control rules with .net codes.

Simple Serialization

ASP.NET web API provides automatic serialization of our classes to the property into JSON format out of the box without any specific configuration. It provides modern web experiences with of course capabilities to customize the serialization for your endpoints based on requirements.

Flexible Routing

ASP.NET gives flexible options to define routes and endpoint URLs from code using attributes. We can define route-based controller names or customize them with different names. We can give different routes for controller methods.

HTTPS Option

APS.NET provides an HTTPS option out of the box. ASP.NET provides this option while creating the project and does the rest of the configuration automatically by generating test certificates for the local development environment. This is one of the important parts of modern applications/ web APIs which impose end-to-end encryption of data over client and server communications and secure APIs with privacy.

Conclusion

ASP.NET provides various options and benefits to build modern web APIs with industry-standard and cross-platform. In this article, we have learned about API and web API which have become the contemporary trends for developing solutions and services. We have also explored ASP.NET web API and created Web API with .NET 5. We have delved into how to implement swagger, test, and debug web API with built-in options for OpenAPI. Furthermore, there are numerous advantages of using ASP.NET for building Web API which is listed above in this article. We will do a complete CRUD operation with a real solution in ASP.NET Web API in the next article.


Similar Articles