ASP.NET MVC User routing like that in StackOverflow?

前端 未结 2 435
醉酒成梦
醉酒成梦 2021-01-02 01:03

I\'ve looked at the routing on StackOverflow and I\'ve got a very noobie question, but something I\'d like clarification none the less.

I\'m looking specifically at

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-02 01:46

    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}
    );
    

提交回复
热议问题