如何使用路由值获取当前 url?

How to get the current url with route values?

我正在尝试使用路由值检索当前请求 url,以便在到达我的控制器时获得具有所有需要值的 return url。

我尝试了 HttpContext.Request.PathHttpContext.Request.GetDisplayUrl() 但它 return 类似于 :

/Home/Products

我真正需要的是检索路由值:

/Home/Products?id=1

有办法实现吗? 谢谢!

你可以做到

HttpContext.Request.Path + HttpContext.Request.QueryString

或者为了方便起见,您可以像这样创建一个扩展方法

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

然后获取电流 URL

var url = HttpContext.Request.GetCurrentUrl();

这篇也许对你有帮助。