2013-03-30 34 views
3

您好,這是某一條mvc4的WebAPI代碼任何人都可以在這裏解釋一下我的每一行的code..I google搜索,但沒有發現任何東西有趣關於mvc4網頁API

public HttpResponseMessage PostProduct(Product item) 
{ 
    item = repository.Add(item); 
    var response = Request.CreateResponse(HttpStatusCode.Created, item); 

    string uri = Url.RouteUrl("DefaultApi", new { id = item.Id }); 
    response.Headers.Location = new Uri(uri); 
    return response; 
} 

我只知道,我送作爲回報產品item..and這個網頁API返回我新添加的產品的反應,但我不明白這2條線特別

string uri = Url.RouteUrl("DefaultApi", new { id = item.Id }); 
     response.Headers.Location = new Uri(uri); 
+0

請務必檢查所有的答案,作爲接受的標記回答的GET操作方法。 –

回答

4
public HttpResponseMessage PostProduct(Product item) 
{ 
    //creates and adds an item to repository(db) 
    item = repository.Add(item); 
    //creates a new httpresponse 
    var response = Request.CreateResponse(HttpStatusCode.Created, item); 
    //creates new uri 
    string uri = Url.RouteUrl("DefaultApi", new { id = item.Id }); 
    //set header for new uri 
    response.Headers.Location = new Uri(uri); 
    return response; 
} 

此行會創建一個新的RouteUrl - >基本上是一個鏈接爲您的迴應標題。

我的建議是,你應該有正式文件從這裏開始:http://www.asp.net/web-api,它爲我工作。有很多事情在這裏研究:http://geekswithblogs.net/JoshReuben/archive/2012/10/28/aspnet-webapi-rest-guidance.aspx

有在這個答案被張貼的例子太多了,這可以幫助你。

·響應碼:默認情況下,Web API框架將響應 狀態碼設置爲200(OK)。但是根據HTTP/1.1協議,當 POST請求導致創建資源時,服務器 應回覆狀態201(創建)。非Get方法應該返回 HttpResponseMessage

·地點:當服務器創建一個資源,它應該包括在響應的Location頭新資源的 URI。

public HttpResponseMessage PostProduct(Product item) 
{ 
    item = repository.Add(item); 

    var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item); 

    string uri = Url.Link("DefaultApi", new { id = item.Id }); 

    response.Headers.Location = new Uri(uri); 

    return response; 
} 
+0

是的,我已經走了throught there..but我無法理解這一點.... //創建新的URI 字符串URI = Url.RouteUrl( 「DefaultApi」,新{ID = item.Id}); //爲新的uri設置標題 response.Headers.Location = new Uri(uri); – Darshan

+0

選中此項:http://forums.asp.net/t/1776795.aspx/1 –

+1

另請參閱:http://www.asp.net/web-api/overview/creating-web-apis/creating-a -web-api-that-supports-crud-operations在創建資源下。這個想法是:當你創建一個新資源時,服務器應該在響應的Location頭部返回資源的URL。 –

2
public HttpResponseMessage PostProduct(Product item) 
//this means that any post request to this controller will hit this action method 
{ 
    item = repository.Add(item); 
    //the posted data would be added to the already defined repository 

    var response = Request.CreateResponse(HttpStatusCode.Created, item); 
    //a responses is created with code 201. which means a new resource was created. 

    string uri = Url.RouteUrl("DefaultApi", new { id = item.Id }); 
    //you get a new url which points to the route names DefaultAPI and provides a url parameter id(normally defined as optional) 

    response.Headers.Location = new Uri(uri); 
    //adds the created url to the headers to the response 

    return response; 
    //returns the response 
} 

通常作爲標準去POST請求被用來創建的實體。並且要放入該實體的數據與請求一起發送。

所以這裏的代碼創建實體,然後在響應中發回上,你可以找到最近創建的實體的URL。這是任何客戶期望遵循標準的人。儘管這是完全必要的。

所以根據這一點,你必須有一個接受id作爲參數並返回product對應於id