博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)
阅读量:7036 次
发布时间:2019-06-28

本文共 3281 字,大约阅读时间需要 10 分钟。

运行应用

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 .

在Visual Studio中,按CTRL+F5运行程序。Visual Studio将运行默认浏览器并导航至, 这个port端口是自动生成。如果你使用的是Chrome,Edge或者Firefox,将直接显示数据。如果你使用IE,IE会提示你打开或保存valuse.json文件。我们输入 将导航到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 attribute. The 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 .

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

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

pmc.png

 

  • 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信息:

pmget.png

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 . 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。

pmcput.png

删除

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

pmd.png

原文链接

转载于:https://www.cnblogs.com/inday/p/6288714.html

你可能感兴趣的文章
Win2003x64系统
查看>>
设计模式 : Template method 模板方法模式 -- 行为型
查看>>
第二十九节,装饰器
查看>>
[LintCode] Valid Palindrome 验证回文字符串
查看>>
jQuery的基本语法
查看>>
javascript 数组实例
查看>>
iOS开发UI篇—CAlayer(创建图层)
查看>>
深入理解javascript事件流
查看>>
通过js写一个消息弹框
查看>>
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
查看>>
Leetcode: Non-overlapping Intervals
查看>>
Spring组件扫描<context:component-scan/>使用详解
查看>>
CodeIgniter(3.1.4)框架使用静态文件(js,css)
查看>>
python练习笔记——用函数对列表奇偶分类,且过程不增加新列表
查看>>
CentOS 6.9永久设置静态路由表以及路由表常用设置
查看>>
spring mvc : 中文传值(post/get)中文乱码
查看>>
Mysql中处理1970年前的日期(unixtime为负数的情况)负数时间戳格式化
查看>>
物联网架构成长之路(24)-Docker练习之Compose容器编排
查看>>
iocp (改天完善)
查看>>
水波探测算法的实现
查看>>