控制器相同的名称作为一个区域 - Asp.Net MVC4 [英] Controller with same name as an area - Asp.Net MVC4

查看:95
本文介绍了控制器相同的名称作为一个区域 - Asp.Net MVC4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主/顶部区域联系人控制器,和我有一个名为区域联系人。

我得到POST 404到联系人控制器如果注册我的区域前注册我的顶级路线:

 保护无效的Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.DefaultBinder =新NullStringBinder();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

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

...大量的重复控制器名称的问题记录,但我还没有找到一个特定的场景,面积相同的名称作为控制器。

...也许一个简单的办法。将AP preciate帮助。 :-D

FWIW,我注册我的联系方式与区域显式的命名空间:

 公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional},
            命名空间:新[] {MyMvcApplication.Controllers}
        );
    }


解决方案

有需要考虑两件事情


  1. 的Application_Start()的方法寄存器区域第一个 AreaRegistration.RegisterAllAreas();


  2. 在名称冲突的情况下,使用了命名空间的 RouteConfig.cs 的文件的 App_Start 的文件夹以及所有(在路线中定义的路线类似的 ContactsAreaRegistration.cs 的)


要复制您的方案,我创建了一个示例应用程序,并能成功地访问如下两个URL:


HTTP://本地主机:1200 /联系人/索引HTTP://本地主机:1200 /联系人/联系人/索引

我的应用程序的结构是这样的:

这里面的 ContactsAreaRegistration.cs 的文件,我们有以下的code:


公共类ContactsAreaRegistration:AreaRegistration
    {
        公众覆盖字符串AREANAME
        {
            得到
            {
                返回联系人;
            }
        }        公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
        {
            context.MapRoute(
                Contacts_default
                联系人/ {控制器} / {行动} / {ID}
                新{行动=索引,ID = UrlParameter.Optional},
                命名空间:新[] {MvcApplication1.Areas.Contacts.Controllers}
            );
        }
    }

希望这会帮助你。如果你需要我可以把我所创建的样本应用程序code。谢谢你。

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

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

    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.

...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

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

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

解决方案

There are two things to consider

  1. In Application_Start() method register areas first 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)

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

http://localhost:1200/Contacts/Index

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

The structure of my application looks like:

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

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.

这篇关于控制器相同的名称作为一个区域 - Asp.Net MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆