Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current URL in ASPCore Middleware?

Is there a way I can access the current Requested URL in ASPCore 2.0 Middleware?

Is there something I can Inject?

like image 618
Michael Avatar asked Aug 08 '17 15:08

Michael


1 Answers

HttpContext object will be passed to the Invoke method of your middleware. You can access the Request property on that.

You can use the GetDisplayUrl extension method or GetEncodedUrl extension method.

public Task Invoke(HttpContext context)
{
    var url1 =context.Request.GetDisplayUrl();
    var url2  = context.Request.GetEncodedUrl();       


    // Call the next delegate/middleware in the pipeline
    return this._next(context);
}

These 2 extension methods are defined in Microsoft.AspNetCore.Http.Extensions namespace. So make sure you have a using statement to include the namespace

using Microsoft.AspNetCore.Http.Extensions;
like image 141
Shyju Avatar answered Jan 03 '23 11:01

Shyju