Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-4asp.net-mvc-3-areasareas

use different name in area url in mvc


I have created a area named "User" in my mvc project. So now i can access that area using the url mysite.com/user.

Now can i change the name of the area in url ? i want to access my area using the url mysite.com/admin

I can do this by changing the folder name of the "user" area. But i need to modify lot of files if i change my folder name. So is there any other way to show different name in url ? using areaRegistration.cs ?


Solution

  • In your UserAreaRegistration file set up something like this:

    public class UserAreaRegistration : 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 }
            );
        }
    }
    

    And point to the controller/action etc that you want.