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

时间:2018-02-01 20:08:50

标签: url asp.net-core asp.net-core-2.0

如何获取当前网址? 我试着这样说:

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和主机/路径

1 个答案:

答案 0 :(得分:1)

您可以使用中间件在Configure内部的Startup.cs中运行您的功能。你可以做到

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