0
我纔剛剛推出ASP.NET路由,並在此刻註冊兩條路線:ASP.NET的WebForms GetVirtualPath總是返回null
routes.MapPageRoute(
"default",
"{Path}/{Name}.aspx{Query}",
"~/Default.aspx",
true,
new RouteValueDictionary { { "Name", "default" } },
new RouteValueDictionary { { "Name", @"[-_\w]+" } });
routes.MapPageRoute(
"home",
"{Name}.aspx{Query}",
"~/Default.aspx",
true,
new RouteValueDictionary { { "Name", "default" } },
new RouteValueDictionary { { "Name", @"[-_\w]+" } });
然而,當我嘗試使用這些路由建立的網址,我總是得到一個異常。我嘗試這樣做:
private string GetVirtualPath(RouteValueDictionary values)
{
return RouteTable.Routes.GetVirtualPath(
null, values).VirtualPath;
}
而且這樣的:
private string GetVirtualPath(RouteValueDictionary values)
{
var wrapper = new HttpContextWrapper(HttpContext.Current);
return RouteTable.Routes.GetVirtualPath(
new RequestContext(wrapper,
RouteTable.Routes.GetRouteData(wrapper)),
values).VirtualPath;
}
無論哪種方式,我得到RouteTable.Routes.GetVirtualPath
一個空引用異常,當我打電話:
GetVirtualPath(new RouteValueDictionary { { "Name", entity.Name } });
甚至:
GetVirtualPath(new RouteValueDictionary { { "Name", entity.Name }, { "Query", string.Empty } });
什麼可能我在這裏做錯了嗎?