.Net Core Web API from scratch with Visual Studio Code
Hi all,
In this blog I'll explain you how to build and test .Net Core Web API.
I assume you have installed Visual Studio Code and C# extension which comes to that IDE.
So, Lets get started.
1. Create a folder, I'll name it as CoreWebApi.
2. Open this folder in VS Code.
3. Open its' terminal by pressing Ctrl + ~
4. Create folder called API and go to that folder in terminal.
5. Type 'dotnet new web' and hit Enter.
And you should get the following message:
The template "ASP.NET Core Empty" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
6. Go to Startup.cs and add services.AddMvc(); to ConfigureServices(IServiceCollection services) method.
So that method should like as bellow:
7. Comment app.Run section in Configure method and add app.UseMvcWithDefaultRoute(); to that method.
So that method should like as bellow:
8. Save and build the project (Ctrl + Shift + B)
9. Resolve problems as bellow:
In this blog I'll explain you how to build and test .Net Core Web API.
I assume you have installed Visual Studio Code and C# extension which comes to that IDE.
So, Lets get started.
1. Create a folder, I'll name it as CoreWebApi.
2. Open this folder in VS Code.
3. Open its' terminal by pressing Ctrl + ~
4. Create folder called API and go to that folder in terminal.
5. Type 'dotnet new web' and hit Enter.
And you should get the following message:
The template "ASP.NET Core Empty" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
6. Go to Startup.cs and add services.AddMvc(); to ConfigureServices(IServiceCollection services) method.
So that method should like as bellow:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }
7. Comment app.Run section in Configure method and add app.UseMvcWithDefaultRoute(); to that method.
So that method should like as bellow:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // app.Run(async (context) => // { // await context.Response.WriteAsync("Hello World!"); // }); app.UseMvcWithDefaultRoute(); }
8. Save and build the project (Ctrl + Shift + B)
9. Resolve problems as bellow:
- Select Yes to the Warn message "Required assets to build and debug are missing from 'TodoApi'. Add them?"
- Select Restore to the Info message "There are unresolved dependencies".
10. Create another folder in API folder called Controllers.
11. Create a C# file inside that folder and name it as UserController.cs
Copy the following code to that file:
using Microsoft.AspNetCore.Mvc; namespace CoreWebApi.API.Controllers { [Route("api/[controller]")] public class UserController : Controller { [HttpGet] [Route("GetUser")] public string GetUser() { return "Prageeth"; } [HttpGet] [Route("GetUserWithId")] public string GetUserWithId(int id) { return "Roshane: " + id.ToString(); } } }
12. Build and Run the project.
13. Test the Api End points with your browser or Postman as bellow:
- http://localhost:5000/api/user/GetUser
- http://localhost:5000/api/user/GetUserWithId?id=2
Hope you got the idea to create a .Net Core Web API project from scratch.
Happy Coding...
Comments
Post a Comment