加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

获取当前ASP.NET Web Api 2操作的URL

发布时间:2020-12-16 04:14:00 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET Web API 2中,如何获取当前操作的Url.以下是说明性示例. [HttpGet][Route("api/someAction")]public SomeResults GetAll(){ var url = /* what to write here*/ ....} 解决方法 ApiController基类的一个属性(必须从中派生自己的控制器)称为Request
在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问题很有用:

> How to get base URL in Web API controller?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读