asp.net-web-api – 在Webapi中使用Url.Link与属性路由2
发布时间:2020-12-15 19:01:52 所属栏目:asp.Net 来源:网络整理
导读:我想在使用webapi 2时向我的http响应添加一个Location头。下面的方法显示了如何使用一个命名的路由。有谁知道你是否可以使用作为webapi 2的一部分发布的属性路由功能创建Url.Link? string uri = Url.Link("DefaultApi",new { id = reponse.Id });httpRespon
我想在使用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 – 安装KB2993928后,ASP.NET MVC4解决方案无法
- asp.net-mvc – 使用Ajax.BeginForm绑定HttpPostedFileBase
- asp.net-mvc – 失败的ASP.NET MVC路由.这是一个错误还是角
- Asp.net:代表(“Action”)可以序列化为控制状态吗?
- ASP.NET页面授权……你是怎么做到的?
- asp.net – JQGrid不显示数据
- 从asp.net mvc生成PDF文件
- mvc6与signalr的任何样品?
- jqGrid过滤器工具栏显示单个列的搜索运算符选择器
- asp.net – 如何从服务器端关闭Modal popup Extender
推荐文章
站长推荐
- asp.net-mvc-3 – MVC3中的富文本区域
- asp.net-mvc – 使用ASP.NET MVC 3.0进行日期验证
- asp.net-mvc – 在EditorFor for child对象中使用
- asp.net – 从web.config中膨胀时,SmtpClient不会
- asp.net-mvc – 通用列表属性的必需属性
- asp.net-mvc – 如何启用创建,同时还禁用Kendo G
- asp.net – 如何跨服务器场实现锁定?
- asp.net – 在Owin应用程序中每个请求的数据缓存
- asp.net中js+jquery添加下拉框值和后台获取示例
- 为什么IIS比ASP.NET Development Server慢?
热点阅读