【翻译】在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方法为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信息:

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

时间: 2024-08-02 15:01:23

【翻译】在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 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 re

在Visual Studio 2005和ASP.NET 2.0中使用强类型数据存取

asp.net|visual|数据 "Never put off until run time what can be done at compile time."David Gries, Compiler Construction for Digital Computers Introduction 作为程序员,我们在学习一些新技术的时候,范例有时候会是我们最大的敌人.指南通常被设计成简单易懂,但同时里面的懒惰.无效率的甚至是危险的代码编写会增多.像这种情况最普遍存在的就是在ADO.

ASP.NET MVC在Visual Studio中的快捷键

在Visual Studio中有一些不错的快捷键,可以帮助我们在ASP.NET MVC Web Project中快速创建 Controller.创建Views以及在Controller Action和View之间切换. ASP.NET MVC快捷键列表如下: -创建Controller:Ctrl+M Ctrl+C -创建View:Ctrl+M Ctrl+V View与Controller Action之间窗口切换:Ctrl+M Ctrl+G 这些快捷键很好用而且容易记,C是Controller的

在Visual Studio中以编程方式自定义SharePoint网站入门

Microsoft Visual Studio 2005 集成开发环境 (IDE) 提供了用于自定义基于 Windows SharePoint Services 的网站的首选环境.例如,您可以创建 Windows 应用程序.控制台应用程序或类库,以及基于 浏览器的 Web 应用程序(在 Visual Studio 中,此应有程序称作"ASP.NET 网站"或"Web 应用程序" )和实现 Windows SharePoint Services 对象模型的 Web 服

如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目

原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio 2012 Visual Studio Express 2012 for Web 与 的Visual Studio 2010 Visual Studio Web发布更新 与 的Visual Web Developer 2010 Express Visual Studio Web发布更新 您可以部署到任

关于visual studio中的一个关于时间增减的问题

问题描述 关于visual studio中的一个关于时间增减的问题 我在做酒店系统的时候,用时间的控件将房客入住的时间和退房时间存入了sql数据库,但现在我要做客人续住,怎样解决将退房时间读出并加上续住天数,延长退房时间,再存入数据库??? 解决方案 dateadd函数http://www.w3school.com.cn/sql/func_dateadd.asp 解决方案二: 关于Visual Studio2005 中的一个问题一个自动生成visual studio工程的脚本visual stu

从Visual Studio中生成Linux设备

本文讲的是从Visual Studio中生成Linux设备,[IT168 云计算频道]近日Novell发布了SUSE Studio:一个用于创建Linux设备(appliance)的工具.与此同时,Mono小组创建了一个插件以从Visual Studio中生成支持SUSE的设备. SUSE Studio的项目经理Nat Friedman将软件设备(software appliance)定义为:所谓软件设备,实际上就是一个完整的应用栈,包括操作系统.应用软件.所需的任何依赖以及与操作相关的配置与数

在dll中寻找Visual Studio中的图标

在以前的一篇文章:WF4.0工作流设计器,在WPF中宿主了一个工作流设计器 ,它的工具栏如下图: 你会发现工具栏上的活动统统没有图标,而在Visual Studio中WF设计器工具 栏如下图:

在Visual Studio中使用Bookmark

在Visual Studio中有一些非常cool的使用BookMark的快捷键.在我认识的开发人员,很多都没有使用过这个功能. 所有写下来,推荐一下这个小功能. Visual Studio中的Bookmark能加速代码导航能力.可能有一些代码,需要频繁的相互切换.通常你可能是滚动页面,找到该代码块的.Visual Studio已经提供了通过使用快捷键,非常快速地移动到指定的代码段.这就是代码的书签功能. 下面是使用该功能的工具按钮,它们位于菜单栏上. 三种方式创建书签:1.通过点击书签图标,2.