C# MVC MapPageRoute和ActionLink
我已经创建了一个页面路由,因此我可以将我的MVC应用程序与我的项目中存在的几个WebForms页面集成:C# MVC MapPageRoute和ActionLink,c#,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,我已经创建了一个页面路由,因此我可以将我的MVC应用程序与我的项目中存在的几个WebForms页面集成: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // register the report routes routes.MapPageRoute("ReportTest", "r
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") %>
以前有人碰到过这个吗?如何解决此问题?我猜您需要在
MapPageRoute
声明中添加一些参数选项。因此,如果您在webforms
目录中有多个webforms页面,这将很好地工作
routes.MapPageRoute ("ReportTest",
"reports/{pagename}",
"~/WebForms/{pagename}.aspx");
PS:您可能还需要查看RouteCollection
另一种选择是使用
<%=Html.RouteLink("Home","Default", new {controller = "Home", action = "Index"})%>
我刚刚遇到了一个非常类似的问题。我的解决方案是给路由系统一个在查找ActionLink匹配项时拒绝页面路由的理由 具体来说,您可以在生成的URL中看到ActionLink创建了两个参数:controller和action。我们可以使用这些方法使我们的“标准”路由(~/controller/action/id)与页面路由不匹配 通过将页面路由中的静态“报告”替换为一个参数,我们将称之为“控制器”,然后添加一个“控制器”必须为“报告”的约束,我们可以为我们的报告获得相同的页面路由,但拒绝任何带有非“报告”的控制器参数的内容
谢谢为了简洁起见,我想避免使用RouteLink,但最终可能不得不使用它。我只是不明白为什么当我使用ActionLink时,页面路由会与我的常规路由匹配。替代解决方案:
<%=Html.RouteLink("Home","Default", new {controller = "Home", action = "Index"})%>
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 }
);
}