简体   繁体   中英

How to get the current url with route values?

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 !

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM