Prerequisites: .NET Core 2.2 SDK, Node.js, VS CODE
If not,
Download .NETCORE 2.2 SDK from here.
To check Dotnet Version Info,
Open cmd.exe
> dotnet --version
> dotnet --info
Download Node from here.
To check Node Version Info,
Open Cmd.exe
> node --version
Download VS Code from here.
Open Visual Studio Code Settings:-
Install C# for Visual Studio Code (powered by OmniSharp)
Install NuGet Package Manager
Create a new project from CLI.
Open cmd.exe
Move to the desired directory. (Learn basic command prompt commands here.)
dotnet new webapi -n MyWebAPICore is the command to create a new web api project.
dotnet new is the command to create a new project in dotnet core.
dotnet new webapi will create a webapi project.
(NOTE: To know all the project types provided by dotnet new, type dotnet new --help. If no type is given to dotnet new command, it will create a console application project as default.)
To provide the name of project, use -n PROJECT_NAME. (NOTE: If no name is given, it will take folder’s name as project name as default.)
Open Visual Studio Code > Open Project Folder.
Folder structure
You can delete the default ValuesController and create your own.
Code of my TestController.cs looks like,
To build the project, In terminal type dotnet build
To run the project, In terminal type dotnet run
Note: By default, it is running in localhost port 5000. You can change it as required.
To see the output, browse to localhost:5000/api/tests.
To configure error handling,
Go to Startup.cs
Set, app.UseDeveloperExceptionPage(); for Development Environment in Configure Method.
You can test exception case by throwing an exception from code
If you are running it in Development environment, you will be able to see the exact error code.
If you are running it in Production Environment, no error detail will be shown.
To implement swagger in your web api project, take reference from here.