MVC 6 Web Api:解决201(已创建)上的位置标头 [英] MVC 6 Web Api: Resolving the location header on a 201 (Created)
问题描述
在Web Api 2.2中,我们可以通过从控制器中返回来返回位置标头URL,如下所示:
In Web Api 2.2, we could return the location header URL by returning from controller as follows:
return Created(new Uri(Url.Link("GetClient", new { id = clientId })), clientReponseModel);
Url.Link(..)
将根据控制器名称GetClient
相应地解析资源URL:
Url.Link(..)
would resolve the resource URL accordingly based on the controller name GetClient
:
在ASP.NET 5 MVC 6的Web Api中,Url
在框架内不存在,但是CreatedResult
构造函数确实具有location参数:
In ASP.NET 5 MVC 6's Web Api, Url
doesn't exist within the framework but the CreatedResult
constructor does have the location parameter:
return new CreatedResult("http://www.myapi.com/api/clients/" + clientId, journeyModel);
如何像在Web API 2.2中一样,无需手动提供该URL就可以解析该URL?
推荐答案
我没有意识到,但是CreatedAtAction()
方法可满足此要求:
I didn't realise it, but the CreatedAtAction()
method caters for this:
return CreatedAtAction("GetClient", new { id = clientId }, clientReponseModel);
确保您的控制器派生自MVC的控制器.
Ensure that your controller derives from MVC's Controller.
这篇关于MVC 6 Web Api:解决201(已创建)上的位置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!