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

时间:2022-09-07 12:19:49

运行应用

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. If you're using Chrome, Edge or Firefox, the data will be displayed. If you're using IE, IE will prompt to you open or save the values.json file. Navigate to the Todocontroller we just created http://localhost:port/api/todo.

在Visual Studio中,按CTRL+F5运行程序。Visual Studio将运行默认浏览器并导航至http://localhost:port/api/values, 这个port端口是自动生成。如果你使用的是Chrome,Edge或者Firefox,将直接显示数据。如果你使用IE,IE会提示你打开或保存valuse.json文件。我们输入http://localhost:port/api/todo 将导航到TodoController。

执行其他的CRUD操作

We'll add Create, Update, and Delete methods to the controller. These are variations on a theme, so I'll just show the code and highlight the main differences. Build the project after adding or changing code.

我们将在Controller中添加Create、Update和Delete方法。模板中已经创建这些方法,我将会高亮我添加的代码。添加或者更改代码后生成项目。

[HttpPost]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
return BadRequest();
}
TodoItems.Add(item);
return CreatedAtRoute("GetTodo", new { id = item.Key }, item);
}

This is an HTTP POST method, indicated by the [HttpPost] attribute. The [FromBody] attribute tells MVC to get the value of the to-do item from the body of the HTTP request.

这使一个HTTP POST方法,使用了HTTPPost特性。FromBody特性告诉了MVC我们从HTTP request中获取to-do项所需要的值。

The CreatedAtRoute method returns a 201 response, which is the standard response for an HTTP POST method that creates a new resource on the server. CreateAtRoute also adds a Location header to the response. The Location header specifies the URI of the newly created to-do item. See 10.2.2 201 Created.

这个CreatedAtRoute方法返回一个201响应,它是当HTTP POST在服务器上创建新资源后的标准响应。CreateAtRoute方法在响应中添加了定位头信息,这个定位头信息提供了这个新对象的URI。详见:10.2.2 201 Created

使用Postman发送一个创建的请求

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

  • Set the HTTP method to POST

  • 设置HTTP方法为POST

  • Tap the Body radio button

  • 点击Body按钮

  • Tap the raw radio button

  • 选中raw选项

  • Set the type to JSON

  • 设置类型为JSON

  • In the key-value editor, enter a Todo item such as {"Name":"<your to-do item>"}

  • 在key-value编辑器中,输入一个Todo项,比如{"Name":"<your to-do item>"}

  • Tap Send

  • 点击Send

Tap the Headers tab and copy the Location header:

点击Headers选项卡,复制Location信息:

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

You can use the Location header URI to access the resource you just created. Recall the GetById method created the "GetTodo" named route:

你可以使用这个定位头信息中的URI访问你刚创建的资源。还记得我们在GetById中创建的"GetTodo"路由:

[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)

更新

[HttpPut("{id}")]
public IActionResult Update(string id, [FromBody] TodoItem item)
{
if (item == null || item.Key != id)
{
return BadRequest();
} var todo = TodoItems.Find(id);
if (todo == null)
{
return NotFound();
} TodoItems.Update(item);
return new NoContentResult();
}

Update is similar to Create, but uses HTTP PUT. The response is 204 (No Content). According to the HTTP spec, a PUT request requires the client to send the entire updated entity, not just the deltas. To support partial updates, use HTTP PATCH.

Update类似于Create,但使用的HTTP Put,响应代码204(无内容)。根据HTTP规范,PUT请求需要客户端发送整个更新实体,而不是部分。如果需要支持部分更新,需要使用HTTP PATCH。

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

删除

[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
var todo = TodoItems.Find(id);
if (todo == null)
{
return NotFound();
} TodoItems.Remove(id);
return new NoContentResult();
}

The response is 204 (No Content).

相应代码为:204.

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

原文链接

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api

【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)的更多相关文章

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

    HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expo ...

  2. &lbrack;转&rsqb;【翻译】在Visual Studio中使用Asp&period;Net Core MVC创建你的第一个Web API应用(一)

    本文转自:https://www.cnblogs.com/inday/p/6288707.html HTTP is not just for serving up web pages. It’s al ...

  3. ASP&period;NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP&period;NET Core MVC 创建首个 Web API

    原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...

  4. 006&period;Adding a controller to a ASP&period;NET Core MVC app with Visual Studio -- 【在asp&period;net core mvc 中添加一个控制器】

    Adding a controller to a ASP.NET Core MVC app with Visual Studio 在asp.net core mvc 中添加一个控制器 2017-2-2 ...

  5. 在 Visual Studio 中部署 ASP&period;NET Core 应用

    另一篇:在 Docker 中手工部署 ASP.NET Core 应用 操作步骤 1. 安装 Docker For Windows(安装之前 Windows 需要 开启 Hyper-V 虚拟机功能 ) ...

  6. 在Chrome&plus;Visual Studio中调试asp&period;net程序很慢的问题(Firefox也有类似问题)

    在Chrome+Visual Studio中调试asp.net程序很慢的问题(Firefox也有类似问题) 今天开始起在Chrome中调试,发现问题主要出在菜单栏(layout文件)中,google了 ...

  7. 002&period;Create a web API with ASP&period;NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp&period;net core mvc 创建一个 web api 程序】

    Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...

  8. 使用 ASP&period;NET Core MVC 创建 Web API(五)

    使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...

  9. 使用 ASP&period;NET Core MVC 创建 Web API(二)

    使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...

随机推荐

  1. 使用C&num;创建快捷方式

    在Windows中创建快捷方式很简单,直接用右键点击文件或文件夹,选择创建快捷方式即可.如果想用C#代码的方式创建,就没有那么方便了,因为.NET框架没有提供直接创建快捷方式的方法. 首先我们看一下快 ...

  2. Extjs 中column的renderer使用方法

    renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) { if (record.get('productT ...

  3. Apache2&period;2与php5&period;17 mysql配置

    php5.217应该用线程安全搬,不然各种无语的Apache打不开,PHPInfo没有Mysql的信息,记得把php.ini放入系统盘Windows目录下,Win764位的libmysql.dll也放 ...

  4. iOS Block 用法 &lpar;1&rpar;-once again

    Block简介: Block的实际行为和Function很像,最大的差别是在可以存取同一个Scope的变量值.Block实体形式如下: ^(传入参数列){行为主体}; Block实体开头是“^”,接着 ...

  5. linux stat系统调用,获取文件信息。

    stat 函数原型: int stat(const char *path, struct stat *buf); struct stat 说明 struct stat { mode_t st_mode ...

  6. Linux文件权限及用户管理

    /etc/passwd文件与 /etc/shadow文件/etc/passwd文件/etc/passwd文件主要存放登录名.UID等用户相关信息,用户登录密码存放在/etc/shadow文件中.例子: ...

  7. Linux的运行级别详细说明

    Linux 7个运行级别    # 0 - 停机(千万不要把initdefault设置为0 )     # 1 - 单用户模式     # 2 - 多用户,但是没有NFS     # 3 - 完全多用 ...

  8. 题目1032:ZOJ

    题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. 输入: 题目包含多组用例,每组用例占一行,包含ZOJ三个 ...

  9. 快速切题 sgu113 Nearly prime numbers 难度&colon;0

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

  10. SQL语言基本操作(聚合函数)

    一.聚合函数 1.标量函数:只能对单个的数字或值进行计算.主要包括字符函数.日期/时间函数.数值函数和转换函数这四类.如LEFT/RIGHT/SUBSTRING/LTRIM/RTRIM/CONCAT/ ...