gpt4 book ai didi

c# - MVC MapPageRoute 和 ActionLink

转载 作者:太空狗 更新时间:2023-10-29 17:48:44 26 4
gpt4 key购买 nike

我已经创建了一个页面路由,因此我可以将我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com