2017-03-28 07:05:06 +08:00
---
2018-04-03 03:22:30 +08:00
title: Create a Web API with ASP.NET Core and Visual Studio Code
2017-09-20 01:50:21 +08:00
author: rick-anderson
2018-01-29 23:21:31 +08:00
description: Build a web API on macOS, Linux, or Windows with ASP.NET Core MVC and Visual Studio Code
2017-03-28 07:05:06 +08:00
ms.author: riande
2018-04-28 05:54:40 +08:00
ms.custom: mvc
2018-07-31 00:50:47 +08:00
ms.date: 07/30/2018
2017-03-28 07:05:06 +08:00
uid: tutorials/web-api-vsc
---
2018-04-03 03:22:30 +08:00
# Create a Web API with ASP.NET Core and Visual Studio Code
2017-03-28 07:05:06 +08:00
2017-06-30 00:42:25 +08:00
By [Rick Anderson ](https://twitter.com/RickAndMSFT ) and [Mike Wasson ](https://github.com/mikewasson )
2018-01-29 05:49:15 +08:00
In this tutorial, build a web API for managing a list of "to-do" items. A UI isn't constructed.
2017-06-30 00:42:25 +08:00
2018-04-28 05:54:40 +08:00
There are three versions of this tutorial:
2017-06-30 00:42:25 +08:00
* macOS, Linux, Windows: Web API with Visual Studio Code (This tutorial)
* macOS: [Web API with Visual Studio for Mac ](xref:tutorials/first-web-api-mac )
* Windows: [Web API with Visual Studio for Windows ](xref:tutorials/first-web-api )
2017-05-25 04:48:25 +08:00
<!-- WARNING: The code AND images in this doc are used by uid: tutorials/web - api - vsc, tutorials/first - web - api - mac and tutorials/first - web - api. If you change any code/images in this tutorial, update uid: tutorials/web - api - vsc -->
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[template files ](../includes/webApi/intro.md )]
2017-03-28 07:05:06 +08:00
2018-03-20 12:04:00 +08:00
## Prerequisites
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[prerequisites ](~/includes/net-core-prereqs-vscode.md )]
2017-03-28 07:05:06 +08:00
## Create the project
From a console, run the following commands:
```console
2018-04-28 05:54:40 +08:00
dotnet new webapi -o TodoApi
code TodoApi
2017-03-28 07:05:06 +08:00
```
2018-04-28 05:54:40 +08:00
The *TodoApi* folder opens in Visual Studio Code (VS Code). Select the *Startup.cs* file.
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
* 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".
2017-03-28 07:05:06 +08:00
2017-03-30 05:15:18 +08:00
<!-- uid: tutorials/first - mvc - app - xplat/start - mvc uses the pic below. If you change it, make sure it's consistent -->
2018-01-02 04:55:25 +08:00
![VS Code with Warn Required assets to build and debug are missing from 'TodoApi'. Add them? Don't ask Again, Not Now, Yes ](web-api-vsc/_static/vsc_restore.png )
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
Press **Debug** (F5) to build and run the program. In a browser, navigate to http://localhost:5000/api/values. The following output is displayed:
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
```json
["value1","value2"]
```
2017-03-28 07:05:06 +08:00
2017-03-29 04:13:07 +08:00
See [Visual Studio Code help ](#visual-studio-code-help ) for tips on using VS Code.
2017-03-28 07:05:06 +08:00
## Add support for Entity Framework Core
2018-07-31 00:50:47 +08:00
:::moniker range=">= aspnetcore-2.1"
Creating a new project in ASP.NET Core 2.1 or later adds the [Microsoft.AspNetCore.App ](https://www.nuget.org/packages/Microsoft.AspNetCore.App ) package reference to the *TodoApi.csproj* file. Add the `Version` attribute, if not already specified.
[!code-xml[ ](first-web-api/samples/2.1/TodoApi/TodoApi.csproj?name=snippet_Metapackage&highlight=2 )]
:::moniker-end
2018-04-28 05:54:40 +08:00
:::moniker range="< = aspnetcore-2.0"
2018-07-31 00:50:47 +08:00
2018-04-28 05:54:40 +08:00
Creating a new project in ASP.NET Core 2.0 adds the [Microsoft.AspNetCore.All ](https://www.nuget.org/packages/Microsoft.AspNetCore.All ) package reference to the *TodoApi.csproj* file:
[!code-xml[ ](first-web-api/samples/2.0/TodoApi/TodoApi.csproj?name=snippet_Metapackage&highlight=2 )]
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
:::moniker-end
There's no need to install the [Entity Framework Core InMemory ](/ef/core/providers/in-memory/ ) database provider separately. This database provider allows Entity Framework Core to be used with an in-memory database.
2017-03-28 07:05:06 +08:00
## Add a model class
2018-04-28 05:54:40 +08:00
A model is an object representing the data in your app. In this case, the only model is a to-do item.
2017-03-28 07:05:06 +08:00
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:
2018-04-28 05:54:40 +08:00
[!code-csharp[ ](first-web-api/samples/2.0/TodoApi/Models/TodoItem.cs )]
2017-03-28 07:05:06 +08:00
2017-07-06 22:58:17 +08:00
The database generates the `Id` when a `TodoItem` is created.
2017-03-28 07:05:06 +08:00
## 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:
2018-04-28 05:54:40 +08:00
[!code-csharp[ ](first-web-api/samples/2.0/TodoApi/Models/TodoContext.cs )]
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[Register the database context ](../includes/webApi/register_dbContext.md )]
2017-03-28 07:05:06 +08:00
## Add a controller
2018-04-28 05:54:40 +08:00
In the *Controllers* folder, create a class named `TodoController` . Replace its contents with the following code:
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[code and get todo items ](../includes/webApi/getTodoItems.md )]
2017-03-29 04:13:07 +08:00
2017-03-28 07:05:06 +08:00
### Launch the app
2018-04-28 05:54:40 +08:00
In VS Code, press F5 to launch the app. Navigate to http://localhost:5000/api/todo (the `Todo` controller we created).
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[jQuery ](../includes/webApi/add-jquery.md )]
2018-04-11 06:42:43 +08:00
[!INCLUDE[last part of web API ](../includes/webApi/end.md )]
2017-03-28 07:05:06 +08:00
2017-03-29 04:13:07 +08:00
## Visual Studio Code help
2018-04-28 05:54:40 +08:00
* [Getting started ](https://code.visualstudio.com/docs )
* [Debugging ](https://code.visualstudio.com/docs/editor/debugging )
* [Integrated terminal ](https://code.visualstudio.com/docs/editor/integrated-terminal )
* [Keyboard shortcuts ](https://code.visualstudio.com/docs/getstarted/keybindings#_keyboard-shortcuts-reference )
2017-03-29 04:13:07 +08:00
2018-04-28 05:54:40 +08:00
* [macOS keyboard shortcuts ](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf )
* [Linux keyboard shortcuts ](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf )
* [Windows keyboard shortcuts ](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf )
2017-03-28 07:05:06 +08:00
2018-04-28 05:54:40 +08:00
[!INCLUDE[next steps ](../includes/webApi/next.md )]