TTCG
TTCG

Reputation: 9113

using Areas in MVC5

I am trying to achieve the routings as follow:

etc...

I like the ideas of using 'Areas' and want to separate all the codes by using Areas. So, I created my Areas structure like the following screnshot enter image description here

and the code in ApplicationAreaRegistration.cs is

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

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

However, I cannot achieve the route I want like

http://example.com/Application/Index

In stead of that it becomes, http://example.com/Application/Application/Index

I tried to change the default routing without {controller} in AreaRegistration

context.MapRoute(
                "Application_default",
                "Application/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

But I got, Controller is required area.

I know I can easily get http://example.com/Application/Index if I put the Controller in root Controllers Folder. But it means I couldn't group my codes like the Areas anymore and it will be seprated across the MVC Folders.

What I would like to know is, whether I can achieve what I want by using the Areas or am I trying to do which is impossible?

Upvotes: 0

Views: 2211

Answers (1)

NightOwl888
NightOwl888

Reputation: 56909

You need to add a default controller name to the route so MVC understands what to put in the controller route value when you take it out of the URL.

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

Upvotes: 1

Related Questions