Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Area Routing is not working

I created an empty MVC4 Application, all things are working fine, after that I add an Area to my Project Named "Moderator". My area routing code is like this:

using System;
using System.Web.Mvc;

namespace EskimoArt.Areas.Moderator
{
    public class ModeratorAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Moderator";
            }
        }

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

And my Global.asx code is like this:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace EskimoArt
{
    // 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);
        }
    }
}

But now I want to access the

> http://localhost/Moderator/Dashboard

It shows an Error Page like this

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: /Moderator

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
like image 634
Waqas Idrees Avatar asked Oct 08 '13 05:10

Waqas Idrees


2 Answers

Its better to add

AreaRegistration.RegisterAllAreas();

in global.asax.cs. You must place in following order.

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

Note one things that RegisterAllAreas() should be after Registering the routes. ie. RouteConfig.RegisterRoutes(RouteTable.Routes); should be before AreaRegistration.RegisterAllAreas(); .I have tried and tested and it worked for me.

For more info: enter link description here

like image 118
Diwas Poudel Avatar answered Sep 24 '22 16:09

Diwas Poudel


I had the same problem. You look to App_Start/RouteConfig.cs and add top this code AreaRegistration.RegisterAllAreas();

public class RouteConfig
{
    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 }
        );
    }
}

Create create Areas/Moderator/Controllers/DashboardController.cs

public class DashboardController : Controller
{
    //
    // GET: /Moderator/Dashboard/

    public ActionResult Index()
    {
        return View();
    }
}

and create

Areas/Moderator/Views/Dashboard/Index.cshtml

You also need to have Web.config in Areas/Moderator/Views/Web.config ...

like image 40
ruda_k Avatar answered Sep 25 '22 16:09

ruda_k