我想在使用webapi 2时向我的http响应添加一个Location头。下面的方法显示了如何使用一个命名的路由。有谁知道你是否可以使用作为webapi 2的一部分发布的属性路由功能创建Url.Link?
string uri = Url.Link("DefaultApi",new { id = reponse.Id }); httpResponse.Headers.Location = new Uri(uri);
提前致谢
解决方法
当使用属性路由时,您可以使用RouteName与Ur.Link。
public class BooksController : ApiController { [Route("api/books/{id}",Name="GetBookById")] public BookDto GetBook(int id) { // Implementation not shown... } [Route("api/books")] public HttpResponseMessage Post(Book book) { // Validate and add book to database (not shown) var response = Request.CreateResponse(HttpStatusCode.Created); // Generate a link to the new book and set the Location header in the response. string uri = Url.Link("GetBookById",new { id = book.BookId }); response.Headers.Location = new Uri(uri); return response; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。