2010-12-14 64 views
9

我创建了一个页面的路线,所以我可以与存在于我的项目数的WebForms页我的MVC应用程序集成:MVC的MapPageRoute和ActionLink的

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    // register the report routes 
    routes.MapPageRoute("ReportTest", 
     "reports/test", 
     "~/WebForms/Test.aspx" 
    ); 

    routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

这就造成一个问题,每当我在使用Html.ActionLink我的看法:

<%: Html.ActionLink("Home", "Index", "Home") %> 

当我在链接出现像浏览器加载页面:

http://localhost:12345/reports/test?action=Index&controller=Home 

有没有人遇到过这个?我怎样才能解决这个问题?

回答

5

我的猜测是你需要添加一些参数选项到MapPageRoute声明。所以如果你在WebForms目录中有多个webforms页面,这个效果很好。

routes.MapPageRoute ("ReportTest", 
         "reports/{pagename}", 
         "~/WebForms/{pagename}.aspx"); 

PS:你可能也想看看的RouteCollection

RouteExistingFiles属性另一种方法是使用

<%=Html.RouteLink("Home","Default", new {controller = "Home", action = "Index"})%> 
+2

谢谢。我想避免使用RouteLink只是为了简洁,但我可能不得不使用它。我只是不明白为什么当我使用ActionLink时页面路线与我的常规路线相匹配。 – Dismissile 2010-12-15 15:33:29

5

我刚做了一个非常类似的问题。我的解决方案是让路由系统有理由在寻找ActionLink的匹配项时拒绝页面路由。

具体而言,您可以在生成的URL中看到ActionLink创建两个参数:控制器和操作。我们可以使用这些方法来使我们的“标准”路由(〜/ controller/action/id)与页面路由不匹配。

通过用参数替换页面路由中的静态“报告”,我们将其称为“控制器”,然后添加“控制器”必须为“报告”的约束,我们为报告获取相同的页面路由,但拒绝任何带有不是“报告”的控制器参数。

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    // register the report routes 
    // the second RouteValueDictionary sets the constraint that {controller} = "reports" 
    routes.MapPageRoute("ReportTest", 
     "{controller}/test", 
     "~/WebForms/test.aspx", 
     false, 
     new RouteValueDictionary(), 
     new RouteValueDictionary { { "controller", "reports"} }); 

    routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
}