How to hide controller name in Url?

后端 未结 2 804
梦谈多话
梦谈多话 2021-01-16 14:25

How to hide controller name in Url?

I use the ASP.NET MVC.

The original url is: http://www.sample.com/Users.mvc/UserDetail/9615

The \"Users\" is con

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 14:54

    MVC recognizes the difference between "{UserID}" and "{id}" so if you are going to have a route with only "{UserID}" in the Url you need to place it first in the list other wise it never gets hit. And make sure the default includes "id" since it will continually loop over "UserDetails" unless the default references id as apposed to UserID. I found this format works for me:

    routes.MapRoute("UserDetails",
           "{UserID}",
           new { controller = "Users", action = "UserDetail", id = "" }
    );
    routes.MapRoute(
           "Default", // Route name
           "{controller}/{action}/{id}", // URL with parameters               
           new { controller = "Account", action = "LogOn", id = "" } // Parameter defaults   
    );
    

提交回复
热议问题