asp.net-web-api – 在Webapi中使用Url.Link与属性路由2
发布时间:2020-05-25 00:52:48 所属栏目:asp.Net 来源:互联网
导读:我想在使用webapi 2时向我的http响应添加一个Location头。下面的方法显示了如何使用一个命名的路由。有谁知道你是否可以使用作为webapi 2的一部分发布的属性路由功能创建Url.Link? string uri = Url.Link(DefaultApi, new { id = reponse.Id });httpResponse
|
我想在使用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;
}
}
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 从ASP.NET在Office Web Apps中打开excel /
- 企业ASP.NET MVC 3架构大纲
- 我如何在ASP.Net Web窗体中模拟/伪造会话对象?
- asp.net – 在Internet Explorer 8中使用ScriptManager.Reg
- asp.net-mvc-3 – 如何在MVC3 / 4中的Html.BeginForm()中添
- asp.net-mvc – IIS 7.0不显示自定义错误页面
- asp.net – 如何在捕获httpwebrequest超时后关闭底层连接
- 何时在ASP.NET管道中初始化会话状态
- asp.net-mvc – 如何访问querystring在ASP.Net MVC视图
- 通过LAN调试ASP.NET云项目
推荐文章
站长推荐
- 如何在asp.net中检测服务器端的浏览器关闭?
- 使用嵌套类的ASP.NET MVC3 JSON模型绑定
- asp.net-web-api – WebAPI:403在发布网站后被禁
- asp.net – requestValidationMode 4.5 vs 2.0
- asp.net – 如何配置IIS以便在连接到SQL Server时
- ASP.NET通过自定义函数实现对字符串的大小写切换
- asp.net-mvc-3 – 分页/排序不适用于部分视图中使
- asp.net-mvc – 如果使用HTML内容,我可以写入内联
- asp.net-mvc – 如何在asp.net mvc中的静态类中获
- asp.net – SQL Server 2005:事务死锁
热点阅读
