关于mvc4 web api

3

你好,这是一段mvc4 webapi代码,请问有人能够解释每行代码的含义吗?我已经在谷歌上搜索过了,但没有找到任何有趣的内容。

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;
}

我只知道我正在发送产品项目的信息..然后这个 Web API 会返回给我新添加的产品的响应,但是我特别不理解这两行。
 string uri = Url.RouteUrl("DefaultApi", new { id = item.Id });
        response.Headers.Location = new Uri(uri);

请确保您检查所有答案并标记一个答案为已接受。 - radu florescu
2个回答

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 此答案中无法发布太多示例,但这些示例可能会对您有所帮助。

· Response code: By default, the Web API framework sets the response status code to 200 (OK). But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201 (Created). Non Get methods should return HttpResponseMessage

· Location: When the server creates a resource, it should include the URI of the new resource in the Location header of the response.

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;
}

是的,我已经看过了这段代码,但是我无法理解它的含义。//创建新的URI string uri = Url.RouteUrl("DefaultApi", new { id = item.Id }); //为新的URI设置头信息 response.Headers.Location = new Uri(uri); - Darshan
1
另请参见:http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations,其中包括创建资源。其思路是:当您创建新资源时,服务器应在响应的位置标头中返回资源的URL。 - Mike Wasson

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。这是任何遵循标准的客户端都会预期的。尽管这并非必需。
因此,根据此,您必须具有一个GET操作方法,该方法接受ID作为参数并返回对应于该ID的产品。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接