简体   繁体   English

在MVC3中,是否可以在不同的区域中使用相同的控制器名称?

[英]Is it possible, in MVC3, to have the same controller name in different areas?

In MVC3, I have the following areas: 在MVC3中,我有以下几个方面:

  • Mobile 移动
  • Sandbox 砂箱

Then i route maps like this: 然后我像这样路由地图:

    context.MapRoute(
        "Sandbox_default",
        "Sandbox/{controller}/{action}/{id}",
        new { controller = "SandboxHome", action = "Index", id = UrlParameter.Optional }

and

    context.MapRoute(
        "Mobile_default",
        "Mobile/{controller}/{action}/{id}",
        new { controller = "MobileHome", action = "Index", id = UrlParameter.Optional }
    );

The problem is this gives urls like: 问题是这给网址如下:

http://localhost:58784/Mobile/MobileHome HTTP://本地主机:58784 /移动/ MobileHome

and

http://localhost:58784/Sandbox/SandboxHome HTTP://本地主机:58784 /沙盒/ SandboxHome

But I want it like this: 但我希望这样:

http://localhost:58784/Mobile/Home HTTP://本地主机:58784 /手机/家
http://localhost:58784/Sandbox/Home HTTP://本地主机:58784 /沙盒/主页

The problem is when I rename the SandboxHome-Controller to Home, and the MobileHome-Controller to Home, which would give the desired URLs, it won't compile, saying it has two classes for HomeController. 问题是当我将SandboxHome-Controller重命名为Home,而MobileHome-Controller重命名为Home时,它将提供所需的URL,它将无法编译,说它有两个类用于HomeController。

How can I have the same controller name in different areas ? 如何在不同区域使用相同的控制器名称?

Yes. 是。

As explained by this blog post: http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx 正如此博客文章所述: http//haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx

Assuming you have a call to RegisterAllAreas and the AreaRegistration files generated by Visual Studio. 假设您调用了RegisterAllAreas和Visual Studio生成的AreaRegistration文件。 All you need to do is the namespace on the default route in global ASAX to prevent conflicts. 您需要做的就是在全局ASAX中使用默认路由上的命名空间来防止冲突。

//Map routes for the main site. This specifies a namespace so that areas can have controllers with the same name
routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[]{"MyProject.Web.Controllers"}
 );

As long as you keep the Area controllers within their own namespaces. 只要将区域控制器保留在自己的名称空间中即可。 This will work. 这会奏效。

Yes it is but you'll have to change your routing: 是的,但你必须改变你的路线:

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

You could as well keep both routes but don't forget to define area in your defaults. 您也可以保留两条路线,但不要忘记在默认值中定义area

Important 重要

Of course you must keep controllers in their own area namespaces: 当然,您必须将控制器保留在它们自己的区域名称空间中:

namespace MyApp.Areas.Mobile.Controllers
{
    public class HomeController : Controller
    {
        ...
    }
}

namespace MyApp.Areas.Sandbox.Controllers
{
    public class HomeController : Controller
    {
        ...
    }
}

Check this link on MSDN and see the walktrough. 检查MSDN上的此链接,然后查看walktrough。 And don't forget to also check out this MSDN article that talks about area registration, because you will have to call RegisterAllAreas() method. 并且不要忘记查看这篇关于区域注册的MSDN文章 ,因为您将不得不调用RegisterAllAreas()方法。

And since you still want to keep original non-area controllers, you should also read this Phil Haack's article how to do just that (Credit should go to @Rob in his answer for pointing to this blog post first). 既然你仍然希望保留原始的非区域控制器,你还应该阅读这篇Phil Haack的文章如何做到这一点(Credit应该在@Rob的回答中首先指向这篇博文)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM