In this blog, we will learn “How to create .NET 5 API using Visual Studio Code?“
What is .NET 5?
.NET 5 is one of the major release after .NET Core 3.1. It was released in November, 2020.
Microsoft, dropped the word “Core” from the name to indicate that this is the future of .NET. It supports more platforms and apps than .NET Core and .NET Framework.
Read this - What’s new in .NET 5 ?
What is .NET Web API?
.Net Web API is a framework for building, consuming HTTP based service. The advantage of Web API is that it can be consumed by a wide range of clients like a web browser and mobile applications.
Create .NET 5 API in Visual Studio Code
To create .NET 5 based application in Visual Studio, we need to have below Tools/ IDE/ Extensions.
- Visual Studio Code – Download VS Code from this link Download Visual Studio Code – Mac, Linux, Windows
- .NET 5 SDK for Visual Studio Code – Download SDK from this link Download .NET Core SDK for Visual Studio Code (microsoft.com)
- C# Extensions – Use to speedup the C# development in Visual Studio Code
- C# for Visual Studio Code (powered by OmniSharp) – This is a lightweight development tools for .NET Core. Great C# editing support, including Syntax Highlighting, IntelliSense.
Direct search – Type this in search box of VS Code extensions @id:ms-dotnettools.csharp
Create .NET 5 API Project using .NET CLI
Create a .NET 5 API project in 5 simple steps.
Step1: Go to your folder on command prompt, type below command and hit enter.
Below command is to create a new Solution.
dotnet new sln
Step2: Next, type below command to create API Project
dotnet new webapi -o MyAPIProject
Step3: Add API project to your solution.
dotnet sln add MyAPIProject
Below screen shot for your reference.
Step4: Open Visual Studio Code and open the folder where your solution is located and your project structure will look like below.
Step5: Open Terminal in your VS Code, type dotnet run and press enter.
Post successful build. By default, your application will work on 5000 and 5001 port.
Open browser and enter url – https://localhost:5000/<ControllerName>
In this case, only controller we have is WeatherForecast
So, URL will be https://localhost:5000/WeatherForecast
If you want to change the route of API then go to Controller file and change route from Controller to Action
Change [Route("[controller]")] to [Route("[action]")]
Now your API end will be, https://localhost:5000/<ActionName>
How to launch .NET 5 API on Specific PORT?
If you do not want your .NET 5 API to run on default port i.e. 5000 and 5001, then you can change it easily.
Open launchSettings.json file , find your API Project name and then change the applicationUrl.
Watch this Video to know how to create .NET 5 API using Visual Studio Code.
Are you preparing for Microsoft Azure AZ 900 Certification Exam? Follow this link - Microsoft Azure Fundamentals (AZ-900) Certification Sample Questions
Keep following https://www.sharepointcafe.net/