【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)
运行应用
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发送一个创建的请求

Set the HTTP method to
POST设置HTTP方法为POSTTap 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信息:

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。

删除
[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.

原文链接
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api
【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)的更多相关文章
- 【翻译】在Visual Studio中使用Asp.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 ...
 - [转]【翻译】在Visual Studio中使用Asp.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 ...
 - ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
		
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
 - 006.Adding a controller to a ASP.NET Core MVC app with Visual Studio -- 【在asp.net core mvc 中添加一个控制器】
		
Adding a controller to a ASP.NET Core MVC app with Visual Studio 在asp.net core mvc 中添加一个控制器 2017-2-2 ...
 - 在 Visual Studio 中部署 ASP.NET Core 应用
		
另一篇:在 Docker 中手工部署 ASP.NET Core 应用 操作步骤 1. 安装 Docker For Windows(安装之前 Windows 需要 开启 Hyper-V 虚拟机功能 ) ...
 - 在Chrome+Visual Studio中调试asp.net程序很慢的问题(Firefox也有类似问题)
		
在Chrome+Visual Studio中调试asp.net程序很慢的问题(Firefox也有类似问题) 今天开始起在Chrome中调试,发现问题主要出在菜单栏(layout文件)中,google了 ...
 - 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.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 创 ...
 - 使用 ASP.NET Core MVC 创建 Web API(五)
		
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...
 - 使用 ASP.NET Core MVC 创建 Web API(二)
		
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
 
随机推荐
- 深入理解最强桌面地图控件GMAP.NET ---离线地图
			
enjoyeclipse 深入理解最强桌面地图控件GMAP.NET ---离线地图 这章会介绍GMAP.NET的核心功能之一:离线地图.这个功能可以满足很多政府项目.保密项目.或者由于种种原因不能上网 ...
 - 在XAMPP上建立多个域名的站点
			
XAMPP默认安装完毕后,站点文件默认放在/xampp/htdocs/ 文件下,并且可以通过http://localhost 进行访问.先前在测试各种程序的时候均是在/xampp/htdocs/ 文件 ...
 - MySQL 批量导入 csv 文件
			
注意编码一致性,如:我的数据库是utf-8编码,csv文档的编码也是utf-8. 导入使用 LOAD DATA LOCAL INFILE 详细用法请参看文档或者书籍. mysql user表结构: ...
 - POJ 1584 A Round Peg in a Ground Hole
			
先判断是不是N多边形,求一下凸包,如果所有点都用上了,那么就是凸多边形 判断圆是否在多边形内, 先排除圆心在多边形外的情况 剩下的情况可以利用圆心到每条边的最短距离与半径的大小来判断 #include ...
 - Git GitHub的使用
			
Git的工作区示意 GIT总结 使用git github也一段时间突然发现还是少了一些总结,那就从这儿开始吧! (1)git的配置,这儿就从单独的安装配置开始 安装:sudo yum install ...
 - (译)Windsor入门教程---第三部分 编写第一个Installer
			
原文:http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx ...
 - mysql忘记密码及无法使用程序连接
			
select(一)忘记root密码 很久之前装了mysql,后来做项目的时候,一直木有用到,就把密码给忘记了. 网上各种找,最终解决了这个问题了,记录下. 1)打开services,找到mysql服务 ...
 - 6种炫酷的CSS3按钮边框动画特效
			
6种炫酷的CSS3按钮边框动画特效Button border animate 用鼠标滑过下面的按钮看看效果! Draw Draw Meet Center Spin Spin Circle Spin T ...
 - (中等) POJ 3280 Cheapest Palindrome,DP。
			
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...
 - 1227: [SDOI2009]虔诚的墓主人
			
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MBSubmit: 1083 Solved: 514[Submit][Stat ...