Create Web API With .NET Core

Introduction

ASP.NET Core is very a lightweight runtime library of Microsoft. It is platform independent and runs on Windows, Linux, and Mac. It is a high-performance library with modular design. It is very easy and supports having several applications on a web server. The goal of .NET Core is having a single code base that can be used to develop and build an application that supports all the platforms. It is very helpful in the below kinds of scenarios.
  • Cross-platform ASP.NET Web Apps
  • Cross-Platform Console apps
  • Cross-Platform Libraries and Framework
  • UWP Apps
I will explain how to create Web API REST service with the help of Core.

Prerequisites

The following software and libraries are required -
  • Microsoft Visual Studio Code Windows x64
  • .Net Core SDK v2.1.3 x64 Installer
  • Runtimes v2.0.4 x64 Installer
  • .NET Core SDK v2.1.3 x64 Installer Runtime v2.0.4
Install the required software from the below link,
Steps to create a new Web API project

Create a new project 
 
Please follow the below steps to create a web API project,
  • Open the command prompt.
  • Type and execute the below commands.
            d:\ mkdir testwebapi
            d:\ cd testwebapi
            d:\ testwebapi> dotnet new webapi
 
All these commands will create one WebAPI project like below.
 
 

And, Web API generated project structure as below.
 
 

Here, Visual Studio Code will give you some warning like below.

 
 
So, select "Yes" to the warning message "Required assets to build and debug are missing from 'TodoApi'. Add them?"
 
Build and Run the project
 
Click on F5 button to build and run the program. The output window will show like below during compilation.
 
 
Now, open the browser and type the following URL.
 
http://localhost:5000/api/values
 
It will give you the below output in the browser.
 

Create a new Controller
 
Create one controller like below.
 
  
Press F5 for building and running the application. Now, open the browser and type the following URL.
 
http://localhost:5000/api/employee/get_employee
 
This URL will give you the following output.
 

Publish the Application
 
Go to MS Visual Studio Code. Click on "View" menu and  then "Debug Console".
 
It will open the Debug Console.
 
 

Or you can publish by using command prompt also.
 
dotnet publish
 


It will publish the project so that you can configure and deploy it on IIS too. Details of the published folder are given below.

 
Inner files of Published folder -
 
 
 
Now, you can configure this published folder on IIS. You can publish this code at a particular location also, such as -
 
D:\testwebapi>dotnet publish -o d:\myrest
 
I have attached the zipped code for more details.

Conclusion

ASP.NET Core is very good for REST services developmet. It can build platform-independent and lightweight applications.

Up Next
    Ebook Download
    View all
    Learn
    View all