我找不到从asp.net路由请求中获取“真实/未路由”网址的通用方法。
这
http://www.mysite.com/news/today
得到:
http://www.mysite.com/news.aspx?date=today
我的代码像:
var currentUrl = HttpContext.Current.Request.Url;
//if url routing is used
if (HttpContext.Current.Request.RequestContext.RouteData!=null)
{
currentUrl = ???
}
我尝试使用GetVirtualPath
,但有时会返回空值,例如使用Microsoft.AspNet.FriendlyUrls
Request.RequestContext.RouteData.Route.GetVirtualPath(Request.RequestContext,Request.RequestContext.RouteData.Values)
答案 0 :(得分:1)
我发现这种解决方案似乎适用于所有情况:
var currentUrl = Request.Url.AbsolutePath;
if (Page.RouteData != null)
{
if (Page.RouteData.Route!=null)
{
var virtualPathData = Page.RouteData.Route.GetVirtualPath(Request.RequestContext, Page.RouteData.Values);
if (virtualPathData != null)
{
//asp.net routing
currentUrl = virtualPathData.VirtualPath;
}
else
{
//only for Microsoft.AspNet.FriendlyUrls
//GetFriendlyUrlFileVirtualPath never returns nulls
currentUrl = Request.GetFriendlyUrlFileVirtualPath().Replace("~", "");
}
}
}
对于查询字符串使用:Request.QueryString