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

在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?

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....