ASP.NET Webforms .NET 4.0中的路由 - GetVirtualPath返回null

时间:2014-07-10 08:33:04

标签: c# asp.net .net-4.0 webforms url-routing

我在名为RouteConfig的静态类中有以下代码,由App_Start调用:

// Create routes with default values for parameters

routes.MapPageRoute(
"SearchResultsRoute",
"SearchResults/{delegatesurname}/{starsid}/{dealercode}/{leadassessorid}/{locationid} /{datefrom}/{dateto}/{statusid}",
"~/SearchResults.aspx",
false,
new RouteValueDictionary
{
    {"delegatesurname", string.Empty},
    {"starsid", string.Empty},
    {"dealercode", string.Empty},
    {"leadassessorid", 0},
    {"locationid", 0},
    {"datefrom", string.Empty},
    {"dateto", string.Empty},
    {"statusid", 0}
}
);

// EnableFriendlyUrls() is removed, as that was blocking the routing for some reason.

我还有一个名为“Navigator”的类,它包含一个构建URL的方法:

protected string GetUrlFromRouteAndParams(string routeName, IDictionary<string,object> parameters)
{
    var routeDict = new RouteValueDictionary(parameters);
    var q = RouteTable.Routes.GetVirtualPath(null, routeName, routeDict);
    return ""; // ignore this line its not pertinent to question
}

现在当我调用上面的GetUrlFromRouteAndParams时,就像这样:

    var pageParams = new Dictionary<string, object>()
    {
        {"delegatesurname", _view.DelegateSurname}                
        {"starsid", _view.STARSID},
        {"dealercode", _view.DealerCode},
        {"leadassessorid", _view.LeadAssessorID},
        {"locationid", _view.LocationID},
        {"datefrom", string.Empty },
        {"dateto", string.Empty},
        {"statusid", 0} 
    };

    var url = navigator.GetUrlFromRouteAndParams("SearchResultsRoute", pageParams);

以上GetUrlFromRouteAndParams中的GetVirtualPath调用返回null。

这是为什么?我该如何解决?

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

//var routeDict = new RouteValueDictionary(parameters);
var q = RouteTable.Routes.GetVirtualPath(null, routeName, null).VirtualPath;
return q.ToString();