Here are steps to create a .NET Core Web API Project using Visual Studio Code in Windows 10.

Fist you need to install the latst SDK for .NET Core that you will get from this URL:

https://dotnet.microsoft.com/en-us/download/dotnet

Choose the first option i.e recommended one.

Install the exe and restart your computer.

Make sure you have installed the VS Code.

Open VS Code and from extension manager, install the following extension:

Create the folder where you want to create the project

Run following command to create the Solution file

dotnet new sln -n "MySolution" code-box

Run the following command to create the Web API Project

dotnet new webapi -n "MySolution" code-box

The projecet has been created with default template. Now run this command to restore the packages.

dotnet restore code-box

Now you can open the project from command prompt in VS Code. Just type the following comamnd and press Enter:

code . code-box

Now open the Terminal in VS Code to run the project

Now to run the project on your local PC use the following command:

dotnet run code-box

The projet will start running on localhost on a port. Copy the URL and enter in any browser like Chrome or Firefox

This Project will not run untill we integrate the Swagger. Swagger is used to give an interface and to write the documentaion of our API.

Look for a method named builder.Services.AddSwaggerGen() in Program.cs file and replace it with the following code:

builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo{ Title = "Swagger Demo", Description = "Demo API", Version = "V1" }); }); code-box

Last step, search for method app.UseSwaggerUI(); in Program.cs file and replace it with the following code:

app.UseSwaggerUI( options => { options.SwaggerEndpoint("/swagger/v1/swagger.json","Swagger Demo"); }); code-box

Now in Terminal write the follwing command and past the URL in your browser

dotnet run code-box

Post a Comment

Previous Post Next Post