Alexander Kravchuk
Alexander Kravchuk

Reputation: 11

MVC 5 Area Hide Home Controller in Url

I have a few areas in MVC 5 and each area has it's own HomeController. So URL looks like:

/domain/{area}/{controller}/{action}/{id}
/domain/myarea/home/myaction

Is it possible to configure routes to hide home controller name for each area? So the URL should look like: /domain/myarea/myaction - pointed to the home controller in area of course.

Thanks, Alex.

Upvotes: 0

Views: 577

Answers (2)

Gino Mortillero
Gino Mortillero

Reputation: 123

this is possible.

You'll need to configure the route as

app.MapControllerRoute(
    name: "root",
    pattern: "{area}/{action}/{id?}",
    defaults: new { area = "MyArea", controller = "Home", action = "MyAction" });

If it matches the default controller, the controller name will not show in the url.

Upvotes: 0

Shyju
Shyju

Reputation: 218852

You should be able to update the route pattern for your area registration to remove the controller name.

For example :

public class EventsAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Events";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Events_default",
            "Events/{action}/{id}",
            new { action = "Index", controller = "Home", id = UrlParameter.Optional },
           new string[] { "MyProject.Areas.Events.Controllers" }
        );
    }
}

Now the request yourSite/Events/Details/23 will return Details action method of HomeController in your Events area.

Upvotes: 0

Related Questions