我想在使用webapi 2时向我的http响应添加Location标头.下面的方法显示了如何使用命名路由执行此操作.有没有人知道您是否可以使用作为webapi 2的一部分发布的属性路由功能创建Url.Link?
string uri = Url.Link("DefaultApi", new { id = reponse.Id });
httpResponse.Headers.Location = new Uri(uri);
Run Code Online (Sandbox Code Playgroud)
提前致谢
Joe*_*ham 55
使用属性路由时,可以将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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12569 次 |
| 最近记录: |