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

7
我在主/顶部区域有一个联系人控制器,并且我有一个名为“联系人”的区域。
如果我在注册顶层路由之前注册我的区域,我会收到针对联系人控制器的POST 404错误。
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.DefaultBinder = new NullStringBinder();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

如�我在路由之�注册我的区域,我的404s到Contacts�制器就会消失,但�在我的Contacts区域的路由是404s。

很多��的�制器�称问题被记录下�了,但我还没有找到一个特定的场景,其中区域��制器��。

�能是一个简�的解决方法。希望得到帮助。😀

顺便说一�,我正在使用显�命�空间注册我的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" }
        );
    }

1
在使用Html.Action等功能时,您是否指定了区域? - Chris Pratt
2个回答

24

有两件事需要考虑:

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

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

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

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

我的应用程序结构如下:

enter image description here

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

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


2
通过使用命名空间重载MapRoute方法可以解决这个问题。 - David J Barnes
非常奇怪,因为它没有解决我的问题。当我访问/Contacts/Index时,它会显示页面未找到。我添加了命名空间。应用程序认为我试图访问/Contacts/Index/Index。我找到的唯一解决方案是从MapRoute方法的defaults参数中删除Action =“Index”或者使用/Contacts URL代替/Contacts/Index - alcohol is evil

0
对于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 }
        );
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接