2011-12-13 37 views
3

叫我有一些地区在我的ASP.NET MVC3应用:区域控制器正在从根本要求

namespace MyProject.Areas.myarea 
{ 
    public class myareaAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "myarea"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "myarea_default", 
       "myarea/{controller}/{action}/{id}", 
       new { action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 
} 

这个区域包含与“微笑”行动“你好”控制器。

在为整个项目Global.asax文件我有:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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

    } 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
    } 

所以,当我要求 “本地主机/ myarea/hello /的微笑”,它调用预期适当的控制器。

但是!当我请求“localhost/hello/smile”时,它会调用hello控制器STILL!因此,它不在myarea/Views文件夹中查找视图,而是在〜/ Views文件夹中查找“root”(非区域)级别的项目。

我该如何解决这个问题,所以服务器会抛出一个404异常,找不到资源,就像我要求的不存在的控制器一样?

UPD:控制器面积在命名空间:

namespace MyProject.Areas.myarea.Controllers 
{ 
    public class HelloController : Controller 

... 
} 

控制器中的 “根” 级是在命名空间:

namespace MyProject.Controllers 
{ 
    public class AnotherRootController : Controller 
... 
} 

所以我在Global.asax中尝试这样做:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Default", 
      "{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      new [] { "MyProject.Controllers" } //Namespace 
     ); 

    } 

我认为这会限制这条路径为“根”-l仅限于evel控制器,因为它们位于MyProject.Controllers命名空间中。这没有用。区域控制器仍然在被请求调用,而没有再次调用。

可能有人能解释,为什么?

+1

在MVC4 “默认” 路线declaraton从Global.asax中移动到〜/ App_Start/RouteConfig.cs /的RegisterRoutes() –

回答

7

在Global.asax中注册默认路由时,您可以设置UseNamespaceFallback=false datatoken,方法是限制它只查看给定名称空间中的控制器。你可以看看following blog post

所以把那个付诸行动添加命名空间限制您的域名注册:为了它限制在给定的注册默认路由时

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "myarea_default", 
     "myarea/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Areas.myarea.Controllers" } 
    ); 
} 

,并在您Global.asax设置UseNamespaceFallback数据令牌假名称空间:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Controllers" } 
    ).DataTokens["UseNamespaceFallback"] = false; 
} 
+0

这工作100%。非常感谢Darin! – Roman

+0

这也帮助我解决了注册默认路由的问题,我希望它在给定区域查看Views。 (使用默认路由时,区域令牌未注册)。使用DataTokens访问器设置“区域”的值就像魅力一样工作。这被我看到的一些评论者认为是一个错误(即使框架不应该支持这一点)。所以感谢Darin。 – M05Pr1mty

相关问题