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


    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.


    public IActionResult Create([FromBody] TodoItem item)
        if (item == null)
            return BadRequest();
        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



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


    You can use the Location header URI to access the resource you just created. Recall the GetById method created the "GetTodo" named route:


    [HttpGet("{id}", Name = "GetTodo")]
    public IActionResult GetById(string 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();
        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。


    public IActionResult Delete(string id)
        var todo = TodoItems.Find(id);
        if (todo == null)
            return NotFound();
        return new NoContentResult();

    The response is 204 (No Content).




    posted @ 2017-01-16 10:33  James.Ying  閱讀(540)  評論(0編輯  收藏  舉報