在ASP.NET Web API 2中,如何获取当前操作的Url.以下是说明性示例.
[HttpGet] [Route("api/someAction")] public SomeResults GetAll() { var url = /* what to write here*/ .... }
解决方法
ApiController基类的一个属性(必须从中派生自己的控制器)称为Request:
// Summary: // Defines properties and methods for API controller. public abstract class ApiController : IHttpController,Idisposable { // ... // // Summary: // Gets or sets the HttpRequestMessage of the current System.Web.Http.ApiController. // // Returns: // The HttpRequestMessage of the current System.Web.Http.ApiController. public HttpRequestMessage Request { get; set; } // ... }
这使您可以访问具有以下方法的HttpRequestMessage:
// // Summary: // Gets or sets the System.Uri used for the HTTP request. // // Returns: // Returns System.Uri.The System.Uri used for the HTTP request. public Uri RequestUri { get; set; }
使用Request.RequestUri获取当前操作的URL.它将返回一个Uri对象,使您可以访问请求的URI的每个部分.
最后,您可能会发现以下SO问题很有用:
原文地址:https://www.jb51.cc/aspnet/247625.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。