【翻譯】在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.html

在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。web

執行其餘的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.json

咱們將在Controller中添加Create、Update和Delete方法。模板中已經建立這些方法,我將會高亮我添加的代碼。添加或者更改代碼後生成項目。api

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

使用Postman發送一個建立的請求

 

  • Set the HTTP method to POSTasp.net

  • 設置HTTP方法爲POSTdom

  • 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

相關文章
相關標籤/搜索