Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 555
Seimann Avatar asked Feb 01 '18 20:02

Seimann


1 Answers

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();
});
like image 76
David Mkheyan Avatar answered Oct 05 '22 07:10

David Mkheyan