如何在NET Core 2.0中获取当前URL

如何获取当前的URL?

我试着这样:

Program.cs中

var location = new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");

要么

public string BuildAbsolute(PathString path, QueryString query = default(QueryString), FragmentString fragment = default(FragmentString))
    {
        var rq = HttpContent.Request;
        return Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildAbsolute(rq.Scheme, rq.Host, rq.PathBase, path, query, fragment);
    }

Visual Studio找不到“请求”

我唯一需要的是获取当前的URL和主机/路径

最佳答案 您可以使用中间件在Configure内部的Startup.cs中运行您的功能.你可以做

app.Use(async (context,next)=>{
     //Do what you want with context,which is HttpContext
     await next.Invoke();
});
点赞