2016-12-23 05:06:46 +08:00
---
2017-05-31 03:38:50 +08:00
title: Create a Web API with ASP.NET Core and Visual Studio for Windows
2016-10-29 01:35:15 +08:00
author: rick-anderson
2017-05-31 03:38:50 +08:00
description: Build a web API with ASP.NET Core MVC and Visual Studio for Windows
2016-10-29 01:35:15 +08:00
manager: wpickett
2018-01-29 23:21:31 +08:00
ms.author: riande
2017-09-20 02:35:11 +08:00
ms.date: 08/15/2017
2017-03-03 08:50:36 +08:00
ms.prod: asp.net-core
2018-01-29 23:21:31 +08:00
ms.technology: aspnet
ms.topic: get-started-article
2016-11-01 13:57:56 +08:00
uid: tutorials/first-web-api
2016-10-29 01:35:15 +08:00
---
2017-03-28 07:05:06 +08:00
2017-08-31 06:57:22 +08:00
#Create a web API with ASP.NET Core and Visual Studio for Windows
2016-10-29 01:35:15 +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-24 23:27:24 +08:00
This tutorial builds a web API for managing a list of "to-do" items. A user interface (UI) isn't created.
2017-06-30 00:42:25 +08:00
There are 3 versions of this tutorial:
* Windows: Web API with Visual Studio for Windows (This tutorial)
* macOS: [Web API with Visual Studio for Mac ](xref:tutorials/first-web-api-mac )
* macOS, Linux, Windows: [Web API with Visual Studio Code ](xref:tutorials/web-api-vsc )
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 -->
2016-10-29 01:35:15 +08:00
2017-05-25 04:48:25 +08:00
[!INCLUDE[intro to web API ](../includes/webApi/intro.md )]
2016-10-29 01:35:15 +08:00
2017-08-16 04:02:52 +08:00
## Prerequisites
2018-03-20 12:04:00 +08:00
[!INCLUDE[ ](~/includes/net-core-prereqs-windows.md )]
2017-08-16 04:02:52 +08:00
2017-05-25 04:48:25 +08:00
## Create the project
2016-10-29 01:35:15 +08:00
2017-03-07 02:41:51 +08:00
From Visual Studio, select **File** menu, > **New** > **Project** .
2016-10-29 01:35:15 +08:00
2018-03-20 12:04:00 +08:00
Select ** .NET Core** > **ASP.NET Core Web Application** project template. Name the project `TodoApi` and select **OK** .
2016-10-29 01:35:15 +08:00
2016-12-23 02:03:29 +08:00
![New project dialog ](first-web-api/_static/new-project.png )
2016-10-29 01:35:15 +08:00
2018-03-09 11:48:07 +08:00
In the **New ASP.NET Core Web Application - TodoApi** dialog, select the **API** template. Select **OK** . Do **not** select **Enable Docker Support** .
2016-10-29 01:35:15 +08:00
2016-12-23 02:03:29 +08:00
![New ASP.NET Web Application dialog with Web API project template selected from ASP.NET Core Templates ](first-web-api/_static/web-api-project.png )
2016-10-29 01:35:15 +08:00
2017-05-25 04:48:25 +08:00
### Launch the app
2017-11-01 06:50:08 +08:00
In Visual Studio, press CTRL+F5 to launch the app. Visual Studio launches a browser and navigates to `http://localhost:port/api/values` , where *port* is a randomly chosen port number. Chrome, Microsoft Edge, and Firefox display the following output:
2017-05-25 04:48:25 +08:00
```
["value1","value2"]
2017-11-01 06:50:08 +08:00
```
2017-05-25 04:48:25 +08:00
2017-08-16 04:02:52 +08:00
### Add a model class
2016-10-29 01:35:15 +08:00
2017-11-01 06:50:08 +08:00
A model is an object that represents the data in the app. In this case, the only model is a to-do item.
2016-10-29 01:35:15 +08:00
Add a folder named "Models". In Solution Explorer, right-click the project. Select **Add** > **New Folder** . Name the folder *Models* .
2017-12-05 12:34:13 +08:00
Note: The model classes go anywhere in the project. The *Models* folder is used by convention for model classes.
2016-10-29 01:35:15 +08:00
2017-03-07 02:41:51 +08:00
Add a `TodoItem` class. Right-click the *Models* folder and select **Add** > **Class** . Name the class `TodoItem` and select **Add** .
2016-10-29 01:35:15 +08:00
2017-11-01 06:50:08 +08:00
Update the `TodoItem` class with the following code:
2016-10-29 01:35:15 +08:00
2018-02-25 00:08:11 +08:00
[!code-csharp[ ](first-web-api/sample/TodoApi/Models/TodoItem.cs )]
2017-03-07 02:41:51 +08:00
2017-07-06 22:58:54 +08:00
The database generates the `Id` when a `TodoItem` is created.
2017-03-03 11:46:08 +08:00
### Create the database context
2017-08-16 04:02:52 +08:00
The *database context* is the main class that coordinates Entity Framework functionality for a given data model. This class is created by deriving from the `Microsoft.EntityFrameworkCore.DbContext` class.
2017-03-03 11:46:08 +08:00
2017-03-07 02:41:51 +08:00
Add a `TodoContext` class. Right-click the *Models* folder and select **Add** > **Class** . Name the class `TodoContext` and select **Add** .
2017-03-03 11:46:08 +08:00
2017-11-01 06:50:08 +08:00
Replace the class with the following code:
2017-08-16 04:02:52 +08:00
2018-02-25 00:08:11 +08:00
[!code-csharp[ ](first-web-api/sample/TodoApi/Models/TodoContext.cs )]
2017-03-03 11:46:08 +08:00
2017-05-25 04:48:25 +08:00
[!INCLUDE[Register the database context ](../includes/webApi/register_dbContext.md )]
2016-10-29 01:35:15 +08:00
2017-08-16 04:02:52 +08:00
### Add a controller
2016-10-29 01:35:15 +08:00
2017-11-01 06:50:08 +08:00
In Solution Explorer, right-click the *Controllers* folder. Select **Add** > **New Item** . In the **Add New Item** dialog, select the **Web API Controller Class** template. Name the class `TodoController` .
2016-10-29 01:35:15 +08:00
2017-04-02 05:30:30 +08:00
![Add new Item dialog with controller in search box and web API controller selected ](first-web-api/_static/new_controller.png )
2017-03-07 02:41:51 +08:00
2017-11-01 06:50:08 +08:00
Replace the class with the following code:
2017-03-07 02:41:51 +08:00
2017-05-25 04:48:25 +08:00
[!INCLUDE[code and get todo items ](../includes/webApi/getTodoItems.md )]
2017-11-01 06:50:08 +08:00
2016-10-29 01:35:15 +08:00
### Launch the app
2017-11-01 06:50:08 +08:00
In Visual Studio, press CTRL+F5 to launch the app. Visual Studio launches a browser and navigates to `http://localhost:port/api/values` , where *port* is a randomly chosen port number. Navigate to the `Todo` controller at `http://localhost:port/api/todo` .
2016-10-29 01:35:15 +08:00
2017-05-25 04:48:25 +08:00
[!INCLUDE[last part of web API ](../includes/webApi/end.md )]
2016-10-29 01:35:15 +08:00
2017-05-25 04:48:25 +08:00
[!INCLUDE[next steps ](../includes/webApi/next.md )]
2016-10-29 01:35:15 +08:00