30

I created MVC Application that have 3 different Area. (Admin, User, News) This is my RouteConfig.cs File in App_Start directory:

public class RouteConfig
{
    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 },
            namespaces: new[] { "TestMvcApplication.Controllers" }
        );
    }
}

And This is my AdminAreaRegisteration.cs file:

    namespace TestMvcApplication.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

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

And finally this is my Global.asax.cs file content:

namespace TestMvcApplication
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

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

Home page of my website fully loaded and it's works. but Homepage of Admin or other areas are not detect by route and I gave this error message:

Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Admin/Home

How can I solve this problem? Thanks.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Mojtaba
  • 1,470
  • 4
  • 18
  • 25
  • 1
    Do you actually have a home controller in your admin area? – James Nov 19 '12 at 13:32
  • Yes, I have a HomeController.cs class for each Area. – Mojtaba Nov 19 '12 at 13:37
  • Does your HomeController have an Index method? Have you overridden the `AreaName` property? – James Nov 19 '12 at 13:50
  • Override code is in my class but I don't copy this property to my question. – Mojtaba Nov 19 '12 at 13:56
  • @Mojitaba word of advice, always copy complete code otherwise people can only work on assumptions...See my answer for what is required for this to work. – James Nov 19 '12 at 13:58
  • The routes look okay, but can you verify that you have a `HomeController.cs` with `Index` action and `Views\Home\Index.cshtml` view in the admin controller? – Kami Nov 19 '12 at 13:41
  • Yes, All of this classes and views has been added to my project. – Mojtaba Nov 19 '12 at 14:00
  • @James Sorry, it's my mistake. I Edited my question and put all of my code into it. – Mojtaba Nov 19 '12 at 14:11

3 Answers3

29

Call AreaRegistration.RegisterAllAreas() somewhere in your RegisterRoutes

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

Tip: Use a tool like RouteDebugger 2.0 or Routing Debugger to investigate your routes

Get latest NuGet: Route Debugger for MVC or RouteDebugger for WepApi

Here's a tutorial on How to set up and use RouteDebugger with WebApi

Lukas Winzenried
  • 1,919
  • 1
  • 14
  • 22
  • 1
    This code is in my Global.asax.cs file and Visual Studio created these methods. – Mojtaba Nov 19 '12 at 14:01
  • 1
    What about Routing Debugger? Could you post the result? – Lukas Winzenried Nov 19 '12 at 14:07
  • I used route debugger but the page could not be loaded and still the problem remains. route debugger only show the routes in loaded pages only. – Mojtaba Nov 20 '12 at 06:50
  • @Mojtaba: Thats why my preference is [Scott's Routing Debugger](http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx) ;-) this one works always, even if the requested page doesn't show up. – Lukas Winzenried Nov 20 '12 at 11:06
  • I added Routing Debugger from NuGet to my project but in this code `RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);` RouteDebugger does not exist. What can I do ? – Mojtaba Nov 20 '12 at 11:58
  • Is there a reference to RouteDebug in your project? – Lukas Winzenried Nov 20 '12 at 12:25
  • The NuGet Package contains a file called RouteDebugger.dll but if you download the [zip](http://code.haacked.com/mvc-1.0/RouteDebug-Binary.zip) it contains RouteDebug.dll. And RouteDebug.dll works fine. – Lukas Winzenried Nov 21 '12 at 22:39
  • I also had the same issue RouteDebugger.dll not found but u just comment the line `RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);` and enable the routedebug in web config and just run it will work fine. It worked for me. – Desmond Nov 29 '12 at 07:55
  • 13
    Why is this marked as the answer?? So you got the debugger to work... What did the debugger tell you? What was the initial problem? What fixed it? – hofnarwillie Sep 26 '13 at 07:11
23

From the code provided I can see 2 potential issues:

  1. You aren't calling RegisterAllAreas
  2. You don't appear to be overriding the AreaName property

Try changing your code to:

Global.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "TestMvcApplication.Controllers" }
    );
}

Admin area

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
James
  • 80,725
  • 18
  • 167
  • 237
5

Just create a static class name it AreaConfig with a static method RegisterAreas() here code:

public static class AreaConfig
{
    public static void RegisterAreas()
    {
        // 
        // Admin area . . .

        var adminArea = new AdminAreaRegistration();
        var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes);
        adminArea.RegisterArea(adminAreaContext);


        // 
        // Default area . . .

        var defaultArea = new DefaultAreaRegistration();
        var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes);
        defaultArea.RegisterArea(defaultAreaContext);
    }
}

then call it in a Global.asax.cs file like this:

protected void Application_Start()
    {
        . . .

        AreaConfig.RegisterAreas();

        . . .
    }
Dirty Coder
  • 114
  • 2
  • 12