Hossein Panahloo
Hossein Panahloo

Reputation: 520

how to change controller name in url in asp.net mvc

I have a "HomeController" and my route is like this

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { 
            controller = "Home", action ="Index", id =    UrlParameter.Optional
            }   
        );

I want url be like "web-design/Index" instead of "Home/Index".
how can i do that ?
thanks

Upvotes: 3

Views: 5996

Answers (2)

Pete
Pete

Reputation: 58412

I would do it like this:

  // add a new route
  routes.MapRoute(
        name: "homepage",
        url: "web-design/{action}",
        defaults: new { 
        controller = "Home", action ="Index"
        }   
    );

  // add your default route but change the default action or controller
  routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
        controller = "Home", action ="SomeotherAction", id =    UrlParameter.Optional
        }   
    );

Upvotes: 4

Hintham
Hintham

Reputation: 1086

If you want "web-design/Index" to be the default route:

routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
        controller = "web-design", action ="Index", id =  UrlParameter.Optional
        }   
    );

Upvotes: 1

Related Questions