【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

时间:2021-08-28 06:20:17

HTTP is not just for serving up web pages. It’s also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop apps.

现在的HTTP协议不再只是为浏览网页而服务,还能构建一个强大的APIs平台提供数据服务。HTTP是简单的、灵活的、无处不在的。几乎你所知的所有平台都有自己的HTTP库,所以HTTP服务拥有众多用户,包括浏览器、移动设备和传统的桌面应用等。

In this tutorial, you’ll build a simple web API for managing a list of "to-do" items. You won’t build any UI in this tutorial.

在本教程中,你将建造一个简单的web api去管理“to-do”项目,在整个过程中不需要构建UI。

ASP.NET Core has built-in support for MVC building Web APIs. Unifying the two frameworks makes it simpler to build apps that include both UI (HTML) and APIs, because now they share the same code base and pipeline.

Asp.Net Core已经内置了使用MVC创建Web APIs。统一了两个框架可以更轻松的创建应用,包括UI(Html)和APIs,因为现在它们共用了相同的基类和管道。 概况

Here is the API that you’ll create:

以下是所需要创建的API:

The following diagram shows the basic design of the app.

以下是这个应用的基础设计图解:

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

创建项目

Start Visual Studio. From the File menu, select New > Project.

打开Visual Studio,从File目录中,选择New > Project。

Select the ASP.NET Core Web Application (.NET Core) project template. Name the project TodoApi, clear Host in the cloud, and tap OK.

选择ASP.NET Core Web Application (.NET Core) 项目模板,名字为:TodoApi,不勾选Host in the cloud,点击OK。

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

In the New ASP.NET Core Web Application (.NET Core) - TodoApi dialog, select the Web API template. Tap OK.

New ASP.NET Core Web Application (.NET Core) - TodoApi对话框中,选择Web Api模板,点击OK。

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

添加一个模型类

Add a folder named "Models". In Solution Explorer, right-click the project. Select Add > New Folder. Name the folder Models.

在解决方案目录中,添加一个名为“Models”文件夹,右键项目-选择Add > New Folder,取名:Models。

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

Add a TodoItem class. Right-click the Models folder and select Add > Class. Name the class TodoItem and tap Add.

添加TodoItem类,右键Models目录,选择Add > Class ,取名:TodoItem,点击添加。

Replace the generated code with:

把以下代码替换自动生成的代码:

namespace TodoApi.Models { public class TodoItem { public string Key { get; set; } public string Name { get; set; } public bool IsComplete { get; set; } } }

添加Repository类

A repository is an object that encapsulates the data layer. The repository contains logic for retrieving and mapping data to an entity model. Even though the example app doesn’t use a database, it’s useful to see how you can inject a repository into your controllers. Create the repository code in the Models folder.

Repository是一个封装了数据访问的对象。这个Repository包含了检索逻辑和数据映射到实体对象的功能。虽然在这个范例中我们不使用数据库,但你能看到在你的controller中注入repository,在Models文件夹中创建Repository代码。

Defining a repository interface named ITodoRepository. Use the class template (Add New Item > Class)

定义一个名为:ITodoRepository的repository接口,,使用类模板(Add New Item > Class)