【问题标题】:How to get the current url with route values?如何使用路由值获取当前 url?
【发布时间】:2020-04-22 16:29:43
【问题描述】:
我正在尝试使用路由值检索当前请求 url,以便在到达我的控制器时获得包含所有需要值的返回 url。
我尝试了HttpContext.Request.Path 和HttpContext.Request.GetDisplayUrl(),但它返回类似:
/Home/Products
我真正需要的是检索路由值:
/Home/Products?id=1
有没有办法做到这一点?
谢谢!
【问题讨论】:
标签:
c#
asp.net-core
request
routevalues
【解决方案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();
这个link 可能对你有帮助。