How to get the current url with route values?

1.7k Views Asked by At

I'm trying to retrieve the current request url with routes values, in order to have a return url with all needed values when reaching my controllers.

I tried HttpContext.Request.Path and HttpContext.Request.GetDisplayUrl() but it returns something like :

/Home/Products

What I actually need is to retrive the routes values to have :

/Home/Products?id=1

Is there a way to achieve that? Thanks !

1

There are 1 best solutions below

0
Farhad Zamani On BEST ANSWER

You can do this

HttpContext.Request.Path + HttpContext.Request.QueryString

Or for convenience you can create an extension method like this

public static string GetCurrentUrl(this HttpRequest httpRequest)
{
    return httpRequest.Path + httpRequest.QueryString;
}

Then get current URL

var url = HttpContext.Request.GetCurrentUrl();

This link maybe helpful for you.