Seimann
Seimann

Reputation: 393

How to get current URL in NET Core 2.0

How to get current URL? I try that:

Program.cs

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

or

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 does not find "Request"

The only thing I need is to take the current URL and Host / Path

Upvotes: 8

Views: 22961

Answers (1)

David Mkheyan
David Mkheyan

Reputation: 528

You can run your functions inside Startup.cs inside Configure using middleware. You can do

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

Upvotes: 3

Related Questions