【问题标题】:Call ActionResult in the Area controller from a shared view从共享视图调用区域控制器中的 ActionResult
【发布时间】:2018-01-27 08:40:02
【问题描述】:

Views 文件夹(区域外)内的 Layout.cshtml 中,我需要解析在区域内创建的控制器的操作方法。

我定义了一些自定义路由,但默认路由一如既往:

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

例如:

Area - Test, Controller - HomeController, Action Method - 索引

@Url.Action("Index", "Home", new { Area = "Test" }) 返回一个空字符串。 @Html.ActionLink("Test link", "Index", "Home") 返回一个空字符串。

@Html.RouteLink("Test Link", "default", new { controller = "Home", action = "Index" }) 错误返回

在路由集合中找不到名为“默认”的路由。

澄清一下,不需要使用自定义路由调用此操作。它永远不会通过 URL 直接访问,因此默认路由应该可以正常工作。

我很沮丧。我该如何解决该操作?

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    在同一命名空间下添加以下类

    public class TestAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Test";
            }
        }
    
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Test_default",
                "Test/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    在 Global.asax 下确保有 RegisterAllAreas()

    protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();            
        }
    

    【讨论】:

    • 我有那个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多