【问题标题】:Controller with same name as an area - Asp.Net MVC4与区域同名的控制器 - Asp.Net MVC4
【发布时间】:2013-11-04 21:50:49
【问题描述】:

我在主/顶部区域有一个联系人控制器,并且我有一个名为“联系人”的区域。

如果我在注册顶级路由之前注册我的区域,我会收到 POST 404 到联系人控制器:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.DefaultBinder = new NullStringBinder();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

而且,如果我在路由之后注册我的区域,我到联系人控制器的 404 将消失,但我到联系人区域的路由现在是 404。

...记录了许多重复的控制器名称问题,但我还没有找到该区域与控制器名称相同的特定场景。

...可能很容易解决。非常感谢帮助。 :-D

fwiw,我正在使用显式命名空间注册我的联系人区域:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MyMvcApplication.Controllers" }
        );
    }

【问题讨论】:

  • Html.Action等时是否指定区域?

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-areas


【解决方案1】:

有两点需要考虑

  1. Application_Start()方法中首先注册区域AreaRegistration.RegisterAllAreas();

  2. 如果名称冲突,请使用 App_Start 文件夹的 RouteConfig.cs 文件中的命名空间以及路由中定义的所有路由(如 ContactsAreaRegistration.cs)

为了复制您的场景,我创建了一个示例应用程序,并且能够成功访问下面给出的两个 URL:

http://localhost:1200/联系人/索引 http://localhost:1200/Contacts/contacts/Index

我的应用程序的结构如下:

ContactsAreaRegistration.cs 文件中,我们有以下代码:

公共类 ContactsAreaRegistration : AreaRegistration { 公共覆盖字符串 AreaName { 得到 { 返回“联系人”; } } 公共覆盖无效RegisterArea(AreaRegistrationContext上下文) { 上下文.MapRoute( "Contacts_default", "联系人/{controller}/{action}/{id}", 新的 { action = "索引", id = UrlParameter.Optional }, 命名空间:新[] {“MvcApplication1.Areas.Contacts.Controllers”} ); } }

希望对您有所帮助。如果您需要,我可以发送我创建的示例应用程序代码。谢谢。

【讨论】:

  • 用命名空间重载 MapRoute 方法解决了这个问题。
  • 这很奇怪,因为它并没有解决我的问题。当我转到/Contacts/Index 时,它说找不到该页面。我添加了命名空间。应用程序认为我尝试去/Contacts/Index/Index。我发现的唯一解决方案是从MapRoute 方法的defaults 参数中删除Action = "Index",或者使用/Contacts URL 而不是/Contacts/Index
【解决方案2】:

对于 MVC5,我做了 @Snesh 所做的,但并没有完全奏效。如果它们具有相同的名称,它只会解析我所在区域中的控制器,而不是项目根目录中的控制器。我最终不得不在RegisterArea 方法和RouteConfig.cs 中的RegisterRoutes 方法中将命名空间指定为参数。

RouteConfig.cs

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            // This resolves to the Controllers folder at the root of the web project
            namespaces: new [] { typeof(Controllers.HomeController).Namespace }
        );
    }

AreaRegistration.cs

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Handheld_default",
            "Handheld/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { typeof(Areas.Handheld.Controllers.HomeController).Namespace }
        );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多