These days, most of the applications are developed using web API because of numerous advantages. Web API gives developers or integration engineers with options to communicate among various applications. Web API decreases the dependencies and make them less coupled among the applications.
Web API is not a technology but a concept and can be built using any technologies 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 utilizations and concepts.
In this article, we will explore in detail about web API, what it is and how to develop web API in asp.net i.e. ASP.NET web API. We will learn following topics
- What is API and Web API
- Http methods
- ASP.NET web API
- Create a 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 API are generally web based which can be accessed over internet using HTTP request. Web API is a kind of web development concept and can be developed using any technologies. It can be consumed by any applications regardless of any programming language. Programmers do not need to know the underlying technologies or programming language of Web API in order to implement or consume it in their solutions.
Google API, for instance, is widely implemented or integrated in several applications such as google map, 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 API for various services like cognitive services, Azure services which provide capabilities to integrate with any applications.
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 .NET framework for building or developing RESTFUL API with HTTP based services which can be accessed via any applications. 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 backed for Single Page Applications, where all the logics, 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 the 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 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 swagger as portrayed.
We can implement swagger by installing NuGet package Swashbuckle.AspNetCore.
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 time required to prepare it
- Reduce the amount needed to connect decoupled services
- Gives UI to test Web API
Web API Controller
API are defined under controller folder with APIController Attribute as shown:
In controller class, we define API controller, Route and http methods as shown sample.
This is how we define the API controller. This is a dummy or sample API, however, in 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 swagger, where I have highlighted some key areas.
- API Endpoint route/URL
- Try it out: To try or test API
- Parameters: this will give option to enter parameter 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 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 flexibility to control policy-based authorization and access control rules with .net codes.
Simple Serialization: ASP.NET web API provides automatic serialization of our classes to 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 URL from code using attributes. We can define route-based controller names or customize it with different names. We can give different routes for controller methods.
HTTPS Option: APS.NET provides HTTPS option out of the box. ASP.NET provides this option while creating the project and do the rest of the configuration automatically by generating test certificates for the local development environment. This is one of the important parts in modern applications/ web APIs which impose end-to-end encryption of data over client and server communications and secure APIs with privacy.
Common .net skills and ecosystem.
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 are 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.