Removing Controller from url in a specific scenario

后端 未结 2 1489
挽巷
挽巷 2021-01-26 01:23

I want to remove Controller named Home from url when user clicks on About and Contact pages in ASP.NET MVC

相关标签:
2条回答
  • 2021-01-26 01:57

    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} );
    
    0 讨论(0)
  • 2021-01-26 02:06
     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.

    0 讨论(0)
提交回复
热议问题