ASP.NET MVC Routing Question

前端 未结 6 2057
悲哀的现实
悲哀的现实 2021-02-02 17:08

I must be dense. After asking several questions on StackOverflow, I am still at a loss when it comes to grasping the new routing engine provided with ASP.NET MVC. I think I\'v

6条回答
  •  温柔的废话
    2021-02-02 18:05

    What about

    routes.MapRoute(
        "Profiles",
        "{userName}",
        new { controller = "Profiles", action = "ShowUser" }
    );
    

    and then, in ProfilesController, there would be a function

    public ActionResult ShowUser(string userName)
    {
    ...
    

    In the function, if no user with the specified userName is found, you should redirect to the default {controller}/{action}/{id} (here, it would be just {controller}) route.

    Urls like www.twitter.com/login should be registered before that one.

    routes.MapRoute(
        "Login",
        "Login",
        new { controller = "Security", action = "Login" }
    );
    

提交回复
热议问题