【问题标题】:Regarding mvc4 web api关于 mvc4 web api
【发布时间】:2016-05-18 23:39:01
【问题描述】:

你好,这是一段 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 会返回我对新添加产品的响应,但我没有特别理解这 2 行

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

【问题讨论】:

  • 请确保您检查所有答案并将答案标记为已接受。

标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api


【解决方案1】:
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;
}

【讨论】:

  • 是的,我已经经历过那里..但我无法理解这一点....//创建新的 uri 字符串 uri = Url.RouteUrl("DefaultApi", new { id = item. ID }); //为新的uri设置标题 response.Headers.Location = new Uri(uri);
  • 另见:asp.net/web-api/overview/creating-web-apis/… 在创建资源下。这个想法是:当你创建一个新资源时,服务器应该在响应的 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。这是任何遵循标准的客户所期望的。虽然这根本没有必要。

所以根据这个你必须有一个GET动作方法,它接受id作为参数并返回对应于idproduct

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多