在Web Api 2.2中,我们可以通过从控制器返回来返回位置标头URL,如下所示:
return Created(new Uri(Url.Link("GetClient",new { id = clientId })),clientReponseModel);
Url.Link(..)将根据控制器名称GetClient相应地解析资源URL:
在ASP.NET 5 MVC 6的Web Api中,Url在框架中不存在,但CreatedResult构造函数确实具有location参数:
return new CreatedResult("http://www.myapi.com/api/clients/" + clientId,journeyModel);
如何在不必手动提供此URL的情况下解析此URL,就像我们在Web API 2.2中所做的那样?
解决方法
我没有意识到,但CreatedAtAction()方法满足于此:
return CreatedAtAction("GetClient",new { id = clientId },clientReponseModel);
确保您的控制器派生自MVC的Controller.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。