与区域同名的控制器 - Asp.Net MVC4

时间:2021-09-07 16:47:06

I have a Contacts controller in the main/top area, and I have an area named "Contacts".

我在主/顶部区域有一个Contacts控制器,我有一个名为“Contacts”的区域。

I get POST 404s to the Contacts controller if I register my areas before I register my top-level routes:

如果我在注册*路线之前注册我的区域,我会将POST 404s发送到Contacts控制器:

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

And, if I register my areas after my routes, my 404s to the Contacts controller goes away, but my routes to the Contacts area are now 404s.

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

...lots of duplicate controller name questions logged, but I haven't found a specific scenario where the area is the same name as the controller.

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

...probably an easy fix. Would appreciate help. :-D

...可能很容易解决。会很感激帮助。 :-D

fwiw, I am registering my Contacts area with an explicit namespace:

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

    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" }
        );
    }

2 个解决方案

#1


24  

There are two things to consider

有两件事需要考虑

  1. In Application_Start() method register areas first AreaRegistration.RegisterAllAreas();.

    在Application_Start()方法中,首先注册AreaRegistration.RegisterAllAreas();.

  2. In case of conflicting name, use the namespaces in RouteConfig.cs file of App_Start folder as well as all the routes defined in routes (like ContactsAreaRegistration.cs)

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

To replicate your scenario, I created a sample application and able to access successfully both URLs given below:

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

http://localhost:1200/Contacts/Index

http://localhost:1200/Contacts/contacts/Index

The structure of my application looks like:

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

与区域同名的控制器 -  Asp.Net MVC4

Here inside ContactsAreaRegistration.cs file we are having following code:

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

public class ContactsAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Contacts";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Contacts_default",
                "Contacts/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MvcApplication1.Areas.Contacts.Controllers" }
            );
        }
    }

Hope it will help you. If you need I can send sample application code which I have created. Thanks.

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

#2


0  

For MVC5, I did what @Snesh did but that didn't fully work. It would only resolve the controllers in my area but not in the root of the project if they had the same name. I wound up having to specify the namespaces as parameters in both the RegisterArea method and the RegisterRoutes method in my RouteConfig.cs.

对于MVC5,我做了@Snesh所做的但是没有完全奏效。它只会解析我所在区域的控制器,但如果它们具有相同的名称,则不会解析项目的根目录。我最后必须在RouteConfig.cs中的RegisterArea方法和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 }
        );
    }

#1


24  

There are two things to consider

有两件事需要考虑

  1. In Application_Start() method register areas first AreaRegistration.RegisterAllAreas();.

    在Application_Start()方法中,首先注册AreaRegistration.RegisterAllAreas();.

  2. In case of conflicting name, use the namespaces in RouteConfig.cs file of App_Start folder as well as all the routes defined in routes (like ContactsAreaRegistration.cs)

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

To replicate your scenario, I created a sample application and able to access successfully both URLs given below:

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

http://localhost:1200/Contacts/Index

http://localhost:1200/Contacts/contacts/Index

The structure of my application looks like:

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

与区域同名的控制器 -  Asp.Net MVC4

Here inside ContactsAreaRegistration.cs file we are having following code:

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

public class ContactsAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Contacts";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Contacts_default",
                "Contacts/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MvcApplication1.Areas.Contacts.Controllers" }
            );
        }
    }

Hope it will help you. If you need I can send sample application code which I have created. Thanks.

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

#2


0  

For MVC5, I did what @Snesh did but that didn't fully work. It would only resolve the controllers in my area but not in the root of the project if they had the same name. I wound up having to specify the namespaces as parameters in both the RegisterArea method and the RegisterRoutes method in my RouteConfig.cs.

对于MVC5,我做了@Snesh所做的但是没有完全奏效。它只会解析我所在区域的控制器,但如果它们具有相同的名称,则不会解析项目的根目录。我最后必须在RouteConfig.cs中的RegisterArea方法和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 }
        );
    }