【问题标题】:Why is @Html.ActionLink leaving off the controller and action portions of the link?为什么@Html.ActionLink 离开了链接的控制器和动作部分?
【发布时间】:2011-06-20 22:27:48
【问题描述】:

我正在 MVC3 应用程序的主 _Layout.cshtml 文件中创建一个链接。

@Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { })

当页面被渲染时,这是生成的 Html

<a href="/Security">Security</a>

如果我点击这个链接,我会得到一个空白页。

我安装了routedebugger,结果如下:

我在 Global.ascx.cs 中有如下默认路由:

 routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web" }// Parameter defaults
        );

以及安全区的路线:

context.MapRoute(
            "Security_default",
            "Security/{controller}/{action}/{id}",
            new { area = "Security", controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
        );       

如果我直接输入以下 URL,http://localhost:1410/Security/home,则会显示正确的页面。

如何让@Html.Action 链接包含 URL 的控制器部分?

我确实有一个 Home 控制器,在应用程序的根部分中有一个 Index 操作,并且必须在路由注册中包含命名空间过滤器。

谢谢你, 基思

【问题讨论】:

    标签: asp.net-mvc-routing html.actionlink routedebugger


    【解决方案1】:

    将安全区域的路由映射更改为以下内容:

    context.MapRoute(
            "Security_default",
            "Security/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
        );    
    

    请注意,默认参数的区域和控制器部分已被删除。

    现在@Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { }) 呈现&lt;a href="/Security/Home"&gt;Security&lt;/a&gt;

    基思

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2016-03-19
      • 2011-02-06
      • 2011-01-22
      • 2023-03-06
      • 1970-01-01
      • 2016-01-18
      相关资源
      最近更新 更多