9
votes

I have an MVC 4 web application which consists some areas. I have a problem with the routing rules of an area named "Catalog". The RouteConfig.cs file is:

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

and Global.asax as follows:

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

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

And CatalogAreaRegistration is something like this:

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

The problem is when i debug, RouteCollection routes does not include rules that are defined in the area. I used routedebugger and saw that routes collection does not consists rules of "Catalog" area. It has only rules in the RouteConfig.

I have no idea what is the problem. Thanks in advance.

2
What if you add area = "Catalog" to the defaults of the MapRoute call in the RegisterArea method?asymptoticFault
@asymptoticFault i tried but it does not matter since rule in the RegisterArea, does not exist in the global routes.atakanozgun
In other words, if i add a break point in the RegisterArea method, it never hits. However, AreaRegistration.RegisterAllAreas() called inside the global.asaxatakanozgun
Clean you app. Rebuild your app and try again.Imad Alazani
Cleaned, rebuilded, restarted, deleted the temp files, tried different pc... I think i'm overlooking something. :(atakanozgun

2 Answers

20
votes

I think due to Visual Studio's caching, some of the dll's aren't compiled properly and this situation can happen. If you do, delete all temp files from following locations:

  • C:\Temp
  • C:\Users\%Username%\AppData\Local\Microsoft\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
  • Path\To\Your\Project\obj\Debug

Update :

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

Then restart the Visual Studio. This is how i resolved.

1
votes

Just add the namespace of your controllers to the 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" }**
        );
    }