So, we have an intern and she is helping us build an internal tool. She is good on the client side, but very light in experience on the back-end. So, I wanted to give her a challenge, Build a .Net Core WebAPI using Visual Studio Code. I wrote up these instructions and she had the API up and a basic understanding of how to iterate it forward in less than an hour. I thought I’d share it in hopes it helps someone else.
Check out Cmder, http://cmder.net/, as an alternative to Windows command prompt.
- Make a directory for the application. I am creating my application in an “api” folder inside my _projects folder. Run
mkdir c:\_projects\api
- Change to your new directory. Run
cd c:\_projects\api
- Create a .Net Core application. Run
dotnet new
- Restore dependencies that are listed in your project.json. Run
dotnet restore
- Open Visual Studio Code and open your application folder. Run
code
- You may see a warning, “Required assets to build and debug are missing from ‘api’. Add them?”, click yes.
- Open the Quick Open (Ctrl+P)
- Run this command “ext install charp”. https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp
- Back in the console you should be able to run the application and see “Hello World!” printed to the console. Run
dotnet run
The project.json currently looks like:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
}
We need to update this to run ASP.Net MVC:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
},
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.Mvc": "1.1.1",
"Microsoft.AspNetCore.Mvc.Core": "1.1.1"
},
"imports": "dnxcore50"
}
}
}
Under frameworks, you will notice that we are running .Net Core 1.1, the current version when this was written. Also, we added some additional dependencies:
- Kestrel – a web server that will serve up your API endpoints to clients
- Mvc – The base ASP.Net Core 1.1.1 dependency
- Mvc.Core – The core ASP.Net Core 1.1.1 depencency
These dependencies will allow us to write and serve our API using ASP.Net Core MVC.
Once you save the project.json Visual Studio Code will let you know “There are unresolved dependencies from ‘project.json’. Please execute the restore command to continue.” You can click “Restore” and you can open the console and run
dotnet restore
This will install the new dependencies that were added to project.json.
Now we need to configure our application to serve our API. We need to update Program.cs from:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
to:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace BInteractive.StoryTeller.Api
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Program>()
.Build();
host.Run();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseMvcWithDefaultRoute();
}
}
}
Here we added new using statements at the top of the class to reference the dependencies we want to use. I changed the namespace to match my application, you can customize the name space to match you application. Normally, I like to have my namespace with MyCompanyName.MyApplicationName.{If the class is in a folder Under my root folder, MyFolderName}.
Now we update the Main method, the entry into the application, to run our API instead of printing “Hello World”. We wire up a host using the Kestrel web server, using this Program class as the start up class, then we build and call run on the host. This starts the server listening and will route based on the configured routes and handle them through the MVC service.
The ConfigureServices method allows you to configure the services you want to use with your API. Right now we only have MVC configured.
The Configure method allows you to inject middle wear into the HTTP pipeline to enhance HTTP request and response handling. You can add things like logging and error pages handling that would work across every request/response.
Now that we are wired up for ASP.Net MVC lets build an API. We are going to build an API that collects and serves questions. So, let define what a question is. Create a new folder under your root folder named “models”. Then create a file name questionmodel.cs.
using System;
namespace BInteractive.StoryTeller.Api.Models
{
public class Question
{
public string Id { get; set; }
public string Title { get; set; }
}
}
This is a plain old CSharp object that has properties to get and set the question Id and Title.
With this we can create a controller that allows clients to work with this model through our API. Create a new folder under your root folder named “controllers”. Then create a file named questioncontroller.cs.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using StoryTeller.Api.Models;
namespace BInteractive.StoryTeller.Api.Controllers
{
[Route("api/[controller]")]
public class QuestionController : Controller
{
private static List<Question> _questions;
static QuestionController()
{
_questions = new List<Question>();
Question question = new Question();
question.Id = "1";
question.Title = "Hello World?";
_questions.Add(question);
}
[HttpGet]
public IEnumerable<Question> GetAll()
{
return _questions.AsReadOnly();
}
[HttpGet("{id}", Name = "GetQuestion")]
public IActionResult GetById(string id)
{
var item = _questions.Find(x => x.Id == id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
[HttpPost]
public IActionResult Create([FromBody] Question item)
{
if (item == null)
{
return BadRequest();
}
item.Id = (_questions.Count + 1).ToString();
_questions.Add(item);
return CreatedAtRoute("GetQuestion", new { controller = "Question", id = item.Id }, item);
}
[HttpDelete("{id}")]
public void Delete(string id)
{
_questions.RemoveAll(n => n.Id == id);
}
}
}
There is a lot here, but the gist is we are setting up an endpoint route for our question API and we are adding methods to get, post, and delete questions. You can dive more into what this is doing by reading up on ASP.Net Core, https://www.asp.net/core.
You should be able to Ctrl-Shift-B to build the application and if everything is good you won’t see any errors. If you are all good you should be able to run the application. In the console go to the application root directory and run
dotnet run
Then you should be able to browse the API at http://localhost:5000/api/question and see a JSON response with the default question of “Hello World?”.
Like this:
Like Loading...