AspNetCore.Docs/aspnetcore/tutorials/first-web-api.md

96 lines
4.0 KiB
Markdown
Raw Normal View History

---
2018-08-09 09:56:56 +08:00
title: Create a Web API with ASP.NET Core and Visual Studio
2016-10-29 01:35:15 +08:00
author: rick-anderson
2018-08-09 09:56:56 +08:00
description: Build a web API with ASP.NET Core MVC and Visual Studio on Windows
2018-01-29 23:21:31 +08:00
ms.author: riande
ms.custom: mvc
ms.date: 05/17/2018
uid: tutorials/first-web-api
2016-10-29 01:35:15 +08:00
---
2018-08-09 09:56:56 +08:00
# Create a Web API with ASP.NET Core and Visual Studio
2016-10-29 01:35:15 +08:00
By [Rick Anderson](https://twitter.com/RickAndMSFT) and [Mike Wasson](https://github.com/mikewasson)
This tutorial builds a web API for managing a list of "to-do" items. A user interface (UI) isn't created.
There are three versions of this tutorial:
2018-08-09 09:56:56 +08:00
* Windows: Web API with Visual Studio on 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)
<!-- 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
[!INCLUDE[intro to web API](../includes/webApi/intro.md)]
2016-10-29 01:35:15 +08:00
## Prerequisites
[!INCLUDE[](~/includes/net-core-prereqs-windows.md)]
## Create the project
2016-10-29 01:35:15 +08:00
Follow these steps in Visual Studio:
2016-10-29 01:35:15 +08:00
* From the **File** menu, select **New** > **Project**.
* Select the **ASP.NET Core Web Application** template. Name the project *TodoApi* and click **OK**.
* In the **New ASP.NET Core Web Application - TodoApi** dialog, choose the ASP.NET Core version. Select the **API** template and click **OK**. Do **not** select **Enable Docker Support**.
2016-10-29 01:35:15 +08:00
### Launch the app
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:
```json
["value1","value2"]
```
If using Internet Explorer, you'll be prompted to save a *values.json* file.
### Add a model class
2016-10-29 01:35:15 +08:00
A model is an object representing the data in the app. In this case, the only model is a to-do item.
2016-10-29 01:35:15 +08:00
In Solution Explorer, right-click the project. Select **Add** > **New Folder**. Name the folder *Models*.
2016-10-29 01:35:15 +08:00
> [!NOTE]
> The model classes can go anywhere in the project. The *Models* folder is used by convention for model classes.
2016-10-29 01:35:15 +08:00
In Solution Explorer, right-click the *Models* folder and select **Add** > **Class**. Name the class *TodoItem* and click **Add**.
2016-10-29 01:35:15 +08:00
Update the `TodoItem` class with the following code:
2016-10-29 01:35:15 +08:00
[!code-csharp[](first-web-api/samples/2.0/TodoApi/Models/TodoItem.cs)]
2017-07-06 22:58:54 +08:00
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. This class is created by deriving from the `Microsoft.EntityFrameworkCore.DbContext` class.
In Solution Explorer, right-click the *Models* folder and select **Add** > **Class**. Name the class *TodoContext* and click **Add**.
Replace the class with the following code:
[!code-csharp[](first-web-api/samples/2.0/TodoApi/Models/TodoContext.cs)]
[!INCLUDE[Register the database context](../includes/webApi/register_dbContext.md)]
2016-10-29 01:35:15 +08:00
### Add a controller
2016-10-29 01:35:15 +08:00
In Solution Explorer, right-click the *Controllers* folder. Select **Add** > **New Item**. In the **Add New Item** dialog, select the **API Controller Class** template. Name the class *TodoController*, and click **Add**.
2016-10-29 01:35:15 +08:00
![Add new Item dialog with controller in search box and web API controller selected](first-web-api/_static/new_controller.png)
Replace the class with the following code:
[!INCLUDE[code and get todo items](../includes/webApi/getTodoItems.md)]
2016-10-29 01:35:15 +08:00
### Launch the app
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
[!INCLUDE[last part of web API](../includes/webApi/end.md)]
2016-10-29 01:35:15 +08:00
[!INCLUDE[jQuery](../includes/webApi/add-jquery.md)]
[!INCLUDE[next steps](../includes/webApi/next.md)]