MVC MapPageRoute 和 ActionLink [英] MVC MapPageRoute and ActionLink

查看:29
本文介绍了MVC MapPageRoute 和 ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个页面路由,以便我可以将我的 MVC 应用程序与我项目中存在的几个 WebForms 页面集成:

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 } 
    );
}

每当我在视图中使用 Html.ActionLink 时,这都会产生一个问题:

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?

推荐答案

我的猜测是您需要在 MapPageRoute 声明中添加一些参数选项.因此,如果您在 WebForms 目录中有多个 Web 表单页面,这会很有效.

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:您可能还想查看 RouteCollection

另一种方法是使用

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

这篇关于MVC MapPageRoute 和 ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆