Introduction
Web APIs are simple Non-SOAP-based HTTP services. We have client-side and server-side, when it comes to server-side, data stored on a database and WEB API acts as an intermediary that fetches data or inserts data to the DB. On the client-side, the actions take place on the user computer. To communicate between client-side and server-side we need REST Apis. ASP.Net Web API is a framework for building HTTP services. We can do different operations using APIs like update invoice details, Insert new user, delete customer order, etc.
Let's start creating a new ASP.Net Core WEB API project.
Step 1
Go to File => New Porject => Select ASP.NET Core Web Application.
Step 2
Add Project Name and Location. Click on Create.
Step 3
From the dropdown, select .NET Core and select API. Click on Create.
Step 4
After creating the project, it should look like the below image. Build and Run the project by clicking on IIS Express.
Step 5
Visual Studio displays the following dialog: Select Yes if you trust the IIS Express SSL certificate. If you have given this permission earlier, these popups won't appear.
The following dialog is displayed: Select Yes and continue.
Step 6
By default, it loads JSON content on the browser.
Step 7
Swagger is the best way to showcase WEB APIs. A Swagger object model exposes SwaggerDocument objects in JSON. After setting up the swagger we start creating an API controller.
We have to Install the package "Swashbuckle.AspNetCore". Right-click on your WebAPi project and click on Manage NuGet Packages.
The below popups will open. Click OK and Accept the License.
After installing the package, update Startup.cs file to enable Swagger.
Insert this to the ConfigureServices function.
-
- services.AddSwaggerGen();
Insert these lines to Configure function.
-
-
- app.UseSwagger(c =>
- {
- c.SerializeAsV2 = true;
- });
-
-
-
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
- c.RoutePrefix = string.Empty;
- });
Step 8
Right-click on the Controller Folder as shown in the below image to create a new controller file.
Select any API Controller. If you select API Controller with read/write actions, by default it will create REST actions to create, read, update, delete APIs.
- API Controller - Empty will create an empty controller.
- API Controller with actions, using Entity Framework will create an API controller with REST actions and list entities from Entity Framework data context.
Give any proper name to the controller.
UserController with a list of GET, POST, PUT, and DELETE APIs.
[Route("api/[Controller ]")] its a default routing system. here api/[controller] is api/name of the controller like api/User.
The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response.
The controller class is derived from ControllerBase class which provides many properties and methods that are useful for handling HTTP requests.
Step 9
By default, launchUrl is set to "api/value". Make it an empty string so that it will load swagger with a list of controllers and corresponding APIs.
Step 10
Run the code by clicking on IIS Express. It will load Controllers with a list of APIs.
You can see GET, POST, PUT, and DELETE with different colors.
Summary
This is the basic flow for creating a WEB API in ASP.NET Core. In the next part, we see how to write API and integrate databases.