问题描述
在Web API 2的模板中,post方法始终像这样:
From the template for Web API 2, a post method is always like this:
[ResponseType(typeof(MyDTO))]
public IHttpActionResult PostmyObject(MyDTO myObject)
{
...
return CreatedAtRoute("DefaultApi", new { id = myObject.Id }, myObject);
}
我不理解这种CreatedAtRoute()
方法.谁能向我解释CreatedAtRoute()
方法?
I don't understand this CreatedAtRoute()
method. Can anyone explain the CreatedAtRoute()
method to me?
推荐答案
CreatedAtRoute
方法用于在调用POST方法存储一些新对象时将URI返回到新创建的资源.因此,例如,如果您发布一个订单商品,则可能会返回一条类似于"api/order/11"的路径(显然11是订单的ID).
The CreatedAtRoute
method is intended to return a URI to the newly created resource when you invoke a POST method to store some new object.So if you POST an order item for instance, you might return a route like 'api/order/11' (11 being the id of the order obviously).
顺便说一句,我同意MSDN文章对理解这一点毫无用处.您实际返回的路由自然取决于您的路由设置.
BTW I agree that the MSDN article is of no use in understanding this. The route you actually return will naturally depend on your routing setup.
这篇关于谁能向我解释CreatedAtRoute()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!