Karthi Dinaa
Karthi Dinaa

Reputation: 13

Areas is not working in mvc 4

i tried to split my module in areas my modules are 1. Login 2. User Admin 3. Master

i created the area and my first start up page is (area = login, controller = login, action = loginpage)

My code route config codes are

RouteConfig.cs

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 = "Login", action = "Login", id = UrlParameter.Optional },
            namespaces: new[] { "SRR.Areas.Login.Controllers" }
        );


    }
}

LoginAreaRegistration.cs

    public override string AreaName
    {
        get
        {
            return "Login";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        //Trace.WriteLine("Login");
        context.MapRoute(
            "Login_default",
            "{controller}/{action}/{id}",
            new { controller = "Login", action = "Login", id = UrlParameter.Optional },
            namespaces: new[] { "SRR.Areas.Login.Controllers" }
        );
    }

UserAdminAreaRegistration.cs

public class UserAdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "UserAdmin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "UserAdmin_default",
            "UserAdmin/{controller}/{action}/{id}",
            new { controller = "Menu", action = "MenuPrivilages", id = UrlParameter.Optional },
            namespaces: new[] { "SRR.Areas.UserAdmin.Controllers" }

        );
    }
}

Here my starting page login is coming fine but the useradmin module is views are not showing it through the error has "The resource cannot be found".

Thanks in advance

Upvotes: 0

Views: 179

Answers (2)

Venkatesh
Venkatesh

Reputation: 1224

The issue that I believe is you have the controller 'login' and action method 'login' twice, one in area and other in root. So that the namespace must differ but you have the similar namespace for both the routes which is wrong. By default it is not necessary to specify the namespace for the default route since it'll map the root files. This might be the reason why the application could not find which log in to call.

Upvotes: 0

Peter Kiss
Peter Kiss

Reputation: 9319

Login area registration missies the area specification in the pattern definition; change "{controller}/{action}/{id}" to "Login/{controller}/{action}/{id}"

What happens if your remove the namespaces constraints from the route registrations? Are the controllers (all of them Login and UserAdmin) in correct namespaces? I guess the Login controllers are showing because of the missing area specification as i mentioned above and the Login* controllers aren't in the correct namespaces.

You RegisterRoutes method misses a line before the default route registration:

   AreaRegistration.RegisterAllAreas();

Upvotes: 1

Related Questions