<input id="ohw05"></input>
  • <table id="ohw05"><menu id="ohw05"></menu></table>
  • <var id="ohw05"></var>
  • <code id="ohw05"><cite id="ohw05"></cite></code>
    <label id="ohw05"></label>
    <var id="ohw05"></var>
  • 【翻譯】在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

    posted @ 2017-01-16 10:33  James.Ying  閱讀(540)  評論(0編輯  收藏  舉報
    国产美女a做受大片观看