ASP.NET MVC User routing like that in StackOverflow?

…衆ロ難τιáo~ 提交于 2019-11-30 12:33:22

Probably just uses a specific route to handle it, also using a regex to specify the format of the ID (so it doesn't get confused with other routes that would contain action names in that position).

// one route for details
routes.MapRoute("UserProfile",
     "Users/{id}/{slug}",
     new { controller = "Users", action = "Details", slug = string.Empty },
     new { id = @"\d+" }
);
// one route for everything else
routes.MapRoute("Default",
     "{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);

Without a SO developer giving a definite answer, reverse engineering could yield many possible combinations and permutations. Here's one that I think would fit too:

routes.MapRoute(
    "UserProfile",
    "Users/{id}/{slug}",
        new { controller = "Users", action = "Profile" }
);

routes.MapRoute(
    "UserLogin",
    "Users/Login",
    new { controller = "Users", action = "Login" }
);

routes.MapRoute(
    "DefaultUser",
    "Users",
    new { controller = "Users", action = "Index" }
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!