c# - MVC MapPageRoute 和 ActionLink

标签 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",
        "reports/test",
        "~/WebForms/Test.aspx"
    );

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

每当我在我的 View 中使用 Html.ActionLink 时,这就会产生一个问题:

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

当我在浏览器中加载页面时,链接显示为:

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

有没有人遇到过这个?我该如何解决这个问题?

最佳答案

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

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

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

另一种方法是使用

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

关于c# - MVC MapPageRoute 和 ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4441222/

相关文章:

c# - 从 Win32_PrintJob 获取总页数

c# - 在 iframe 中使用 .net core 2 AuthorizationHandler 时,为什么身份为 null

c# - 电话号码标准化 : Any pre-existing libraries?

c# - 如何使用c#添加递归函数从Mysql获取列

c# - ASP.NET MVC 复杂模型更新

c# - 转换没有时区的 JSON 日期

asp.net - 有没有办法在 MVC2 中更新 ViewModel?

c# - ASP.NET MVC 2 Html.TextAreaFor Html.TextArea 奇怪的绑定(bind)

javascript - 如何在 JavaScript 中使用 JSON.parse() 解析 HTML 字符串?

asp.net-mvc - ASP.NET MVC : How to display strongly typed view model, 包含项目列表,其中也包含项目列表?