AreaRegistration.RegisterAllAreas()不是注册区域规则

时间:2013-08-19 12:15:03

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我有一个MVC 4 Web应用程序,其中包含一些区域。我对名为“Catalog”的区域的路由规则有疑问。 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 },
        );
    }

和Global.asax如下:

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

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

CatalogAreaRegistration就像这样:

public class CatalogAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Catalog";
        }
    }

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

问题是当我调试时,RouteCollection路由不包括在该区域中定义的规则。我使用routedebugger并看到路由集合不包含“目录”区域的规则。它只有RouteConfig中的规则。

我不知道是什么问题。提前致谢。

2 个答案:

答案 0 :(得分:19)

我认为由于Visual Studio的缓存,一些dll没有正确编译,这种情况可能会发生。如果这样做,请从以下位置删除所有临时文件:

  • C:\ Temp
  • C:\用户\%USERNAME%\应用程序数据\本地\微软\ VisualStudio的
  • C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Temporary ASP.NET Files
  • C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Temporary ASP.NET Files
  • 路径\至\您\项目\ OBJ \调试

更新:

  • AppData \ Local \ Temp \ Temporary ASP.NET Files

然后重新启动Visual Studio。这就是我的决心。

答案 1 :(得分:1)

只需将控制器的命名空间添加到AreaRegistration:

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            **namespaces: new string[] { "Web.Admin.Controllers" }**
        );
    }