MVC MapPageRoute 和 ActionLink

发布于 2024-10-07 13:03:03 字数 823 浏览 2 评论 0原文

我创建了一个页面路由,以便我可以将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

固执像三岁 2024-10-14 13:03:03

我的猜测是,您需要向 MapPageRoute 声明添加一些参数选项。因此,如果 WebForms 目录中有多个 Webforms 页面,那么这种方法效果很好。

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

PS:您可能还想查看 RouteCollectionRouteExistingFiles 属性

另一种方法是使用

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

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 the WebForms directory this works well.

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

PS: You may also want to have a look at the RouteExistingFiles property of RouteCollection

An alternative would be to use

<%=Html.RouteLink("Home","Default", new {controller = "Home", action = "Index"})%>
优雅的叶子 2024-10-14 13:03:03

我刚刚遇到了一个非常相似的问题。我的解决方案是为路由系统提供一个在查找 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 } 
    );
}

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".

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 } 
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文