5.1 KiB
title | author | description | manager | ms.author | ms.custom | ms.date | ms.prod | ms.technology | ms.topic | uid |
---|---|---|---|---|---|---|---|---|---|---|
Create a Web API with ASP.NET Core and Visual Studio Code | rick-anderson | Build a web API on macOS, Linux, or Windows with ASP.NET Core MVC and Visual Studio Code | wpickett | riande | mvc | 05/08/2018 | asp.net-core | aspnet | get-started-article | tutorials/web-api-vsc |
Create a Web API with ASP.NET Core and Visual Studio Code
By Rick Anderson and Mike Wasson
In this tutorial, build a web API for managing a list of "to-do" items. A UI isn't constructed.
There are three versions of this tutorial:
- macOS, Linux, Windows: Web API with Visual Studio Code (This tutorial)
- macOS: Web API with Visual Studio for Mac
- Windows: Web API with Visual Studio for Windows
[!INCLUDEtemplate files]
Prerequisites
[!INCLUDEprerequisites]
Create the project
From a console, run the following commands:
dotnet new webapi -o TodoApi
code TodoApi
The TodoApi folder opens in Visual Studio Code (VS Code). Select the Startup.cs file.
- 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".
Press Debug (F5) to build and run the program. In a browser, navigate to http://localhost:5000/api/values. The following output is displayed:
["value1","value2"]
See Visual Studio Code help for tips on using VS Code.
Add support for Entity Framework Core
:::moniker range="<= aspnetcore-2.0" Creating a new project in ASP.NET Core 2.0 adds the Microsoft.AspNetCore.All package reference to the TodoApi.csproj file:
[!code-xml] :::moniker-end :::moniker range=">= aspnetcore-2.1" Creating a new project in ASP.NET Core 2.1 or later adds the Microsoft.AspNetCore.App package reference to the TodoApi.csproj file:
There's no need to install the Entity Framework Core InMemory database provider separately. This database provider allows Entity Framework Core to be used with an in-memory database.
Add a model class
A model is an object representing the data in your app. In this case, the only model is a to-do item.
Add a folder named Models. You can put model classes anywhere in your project, but the Models folder is used by convention.
Add a TodoItem
class with the following code:
The database generates the Id
when a TodoItem
is created.
Create the database context
The database context is the main class that coordinates Entity Framework functionality for a given data model. You create this class by deriving from the Microsoft.EntityFrameworkCore.DbContext
class.
Add a TodoContext
class in the Models folder:
[!INCLUDERegister the database context]
Add a controller
In the Controllers folder, create a class named TodoController
. Replace its contents with the following code:
[!INCLUDEcode and get todo items]
Launch the app
In VS Code, press F5 to launch the app. Navigate to http://localhost:5000/api/todo (the Todo
controller we created).
[!INCLUDEjQuery]
[!INCLUDElast part of web API]
Visual Studio Code help
[!INCLUDEnext steps]