2012-05-17 38 views
6

我新的ASP.NET MVC3。错误路径越来越拾起并ActionLink的是产生错误的超级链接

我已经配置在Global.asax中某些路线,针对其我生成使用@ Html.ActionLink辅助方法中的一些超链接。

所有链接都得到正确呈现除了最上面的一个在下面的代码:

的Global.asax

routes.MapRoute(
    null, 
    "Section/{Page}/{SubPage}/{DetailPageName}", 
    new { controller = "Base" } 
    ); 

routes.MapRoute(
    null, 
    "Section/{Page}/{SubPage}", 
    new { controller = "Base", action = "SubPage" } 
    ); 

routes.MapRoute(
    null, 
    "Section/{Page}", 
    new { controller ="Base", action="LandingPage"} 
    ); 

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}", // URL with parameters 
    new { controller = "Base", action = "Index" } // Parameter defaults 
    ); 

ActionLink的代码

@Html.ActionLink(@subPages.LinkedPageName, "DetailPage", 
    new { 
     Controller = "Base", 
     Page = @ViewBag.PageName, 
     SubPage = @Model.SubPageName, 
     DetailPageName = subPages.LinkedPageName 
    }) 

上面应该选择最高路线,即:

routes.MapRoute(
    null, 
    "Section/{Page}/{SubPage}/{DetailPageName}", 
    new { controller = "Base" } 
    ); 

但它正在复苏的默认路由!

+0

我注意到在您的第一条路线没有定义'action'。 – Jacob

回答

2

在这个路由定义:

routes.MapRoute(
    null, 
    "Section/{Page}/{SubPage}/{DetailPageName}", 
    new { controller = "Base" } 
    ); 

下列条件必须得到满足,以使路由匹配:

  1. 如果传递到ActionLink一个controller参数然后将其值必须是Base
  2. Page参数必须指定,并且必须非空,因为它没有默认值
  3. SubPage参数必须指定,必须是非空的,因为它没有默认值
  4. 必须指定DetailPageName参数必须为非空的,因为它没有默认值

所以在这种致电ActionLink

@Html.ActionLink(@subPages.LinkedPageName, "DetailPage", 
    new { 
     Controller = "Base", 
     Page = @ViewBag.PageName, 
     SubPage = @Model.SubPageName, 
     DetailPageName = subPages.LinkedPageName 
    }) 

明确满足条件#1。但条件#2,#3和#4可能不满足,因为它们的值可能为空。

因为你指出的是最终匹配的路由是默认路由,我怀疑是Page参数为空或空。即@ViewBag.PageName正在返回空值或空值。

检查你的代码(也许在调试器或打印出来视图)看PageName财产是否具有价值。