MVC MapPageRoute 和 ActionLink
我创建了一个页面路由,以便我可以将我的 MVC 应用程序与项目中存在的一些 WebForms 页面集成:
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
以前有人遇到过这个吗?我该如何解决这个问题?
I have created a page route so I can integrate my MVC application with a few WebForms pages that exist in my project:
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 }
);
}
This has created a problem whenever I use Html.ActionLink in my Views:
<%: Html.ActionLink("Home", "Index", "Home") %>
When I load the page in the browser the link appears like:
http://localhost:12345/reports/test?action=Index&controller=Home
Has anyone run into this before? How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,您需要向
MapPageRoute
声明添加一些参数选项。因此,如果WebForms
目录中有多个 Webforms 页面,那么这种方法效果很好。PS:您可能还想查看
RouteCollection
的RouteExistingFiles
属性另一种方法是使用
My guess is that you need to add some parameter options to the
MapPageRoute
declaration. So if you have more than one webforms page in theWebForms
directory this works well.PS: You may also want to have a look at the
RouteExistingFiles
property ofRouteCollection
An alternative would be to use
我刚刚遇到了一个非常相似的问题。我的解决方案是为路由系统提供一个在查找 ActionLink 匹配项时拒绝页面路由的理由。
具体来说,您可以在生成的 URL 中看到 ActionLink 创建了两个参数:控制器和操作。我们可以使用这些作为使我们的“标准”路由(~/controller/action/id)与页面路由不匹配的方法。
通过用一个参数替换页面路由中的静态“报告”,我们将调用“控制器”,然后添加一个约束,即“控制器”必须是“报告”,我们为报告获得相同的页面路由,但拒绝任何带有不是“报告”的控制器参数。
I just had a very similar issue. My solution was to give the routing system a reason to reject page route when looking for matches for the ActionLink.
Specifically, you can see in the generated URL that the ActionLink creates two parameters: controller and action. We can use those as a method to make our "standard" routes (~/controller/action/id) not match the page route.
By replacing the static "reports" in the page route with a parameter we'll call "controller" and then adding a constraint that the "controller" must be "reports" we get the same page route for our reports, but reject anything with a controller parameter that isn't "reports".