0

I want to remove Controller named Home from url when user clicks on About and Contact pages in ASP.NET MVC sample application. I tried this but it is giving me a 404 error.

routes.MapRoute("Home", "{action}/{id}",
    new { controller = "Home" });

When i remove this all works perfectly.
Note I want to remove Controller name only when Controller is Home. Other Controller should remain same. Further I kept the code in Route.Config file above

 routes.MapRoute("Home", "{action}/{id}",
     new { controller = "Home" });

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

Required Result

abc.com/Home/About should shown as abc.com/About
abc.com/Home/Contact should shown as abc.com/Contact

syed mohsin
  • 2,948
  • 2
  • 23
  • 47

2 Answers2

1

You should map new route in the global.asax (add it before the default one), for example:

routes.MapRoute("SpecificRouteforHomeController", "{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional});

// default route  any defalt you want
routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Account", action = "Login", id = UrlParameter.Optional} );
  • this is specific for Home/Index. I want it for Home/About and Home/Contact – syed mohsin Sep 05 '13 at 04:40
  • 2
    Rex it is not working with any other controller except home and Account. What i want is it remove controller when controller name is website and pass all remaining controller and actions – syed mohsin Sep 06 '13 at 11:56
-2
 routes.MapRoute(
 name: "Default",
 url: "{controller}/{action}/{id}",
 defaults: new { controller = "DefaultControllerName", action = "Index", id = UrlParameter.Optional }
    );

If u will specify the controller name then this will work with that controller name. Bu if not the by default it will take controller name as DefaultControllerName.

Anirudh Agarwal
  • 655
  • 2
  • 7
  • 29