是否有可能,在MVC3,有在不同领域的同一个控制器的名字? [英] Is it possible, in MVC3, to have the same controller name in different areas?

查看:113
本文介绍了是否有可能,在MVC3,有在不同领域的同一个控制器的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC3,我有以下几个方面:

In MVC3, I have the following areas:


      
  • 移动

  •   
  • 沙盒

  •   

然后我路线图是这样的:

Then i route maps like this:

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

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

问题是这样给出的网址,如:

The problem is this gives urls like:

的http://本地主机:58784 /移动/ MobileHome

的http://本地主机:58784 /沙盒/ SandboxHome

但我想它是这样的:

的http://本地主机:58784 /手机/家

  的http://本地主机:58784 /沙盒/主页

问题是,当我重​​新命名SandboxHome控制器来家,而MobileHome控制器来家,这将使所需的网址,它不会编译,说这有两个班的HomeController。

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.

我怎样才能在不同的领域有相同的控制器名称?

How can I have the same controller name in different areas ?

推荐答案

是的。

正如这篇博客文章解释说:<一href=\"http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx\">http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx

As explained by this blog post: http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx

假设你已经到RegisterAllAreas和由Visual Studio生成的AreaRegistration文件的调用。所有你需要做的是在全球ASAX至prevent冲突的默认路由的命名空间。

Assuming you have a call to RegisterAllAreas and the AreaRegistration files generated by Visual Studio. All you need to do is the namespace on the default route in global ASAX to prevent conflicts.

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

这篇关于是否有可能,在MVC3,有在不同领域的同一个控制器的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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