1

我正在构建一个小型 MVC 应用程序。用户登录后,我希望显示他/她的路线:

www.appname.com/username/

当然,每个用户都会调用相同的操作,例如/home/index。我该如何编写我MapRoute的代码来实现这一点以及我应该使用哪些其他代码(属性)?

4

1 回答 1

1

将此路线添加到global.asax.cs文件中的路线

  routes.MapRoute(
            "RouteName", // Route name
            "FixedUrlSegment/{UserName}/{Controller}/{action}/{id}/", // URL with parameters
            new { controller = "ControllerName", 
                  action = "ActionName",
                  id=UrlParameter.Optional
                }, 
        );

我认为您应该使用固定路段作为路线的起点,以将其与默认路线或其他路线区分开来

当然,在登录操作方法中,您必须重定向到该新路线

return RedirectToRoutePermanent("RouteName", new { username = "UserName",
                                                   action = "Index", 
                                                   controller = "Home",
                                                   id="userId"
                                                 }
                                );
// remember id is not required for that route as mentioned in global file

此示例会将您的页面重定向到 url

www.appname.com/FixedUrlSegment/loggedusername/home/index/loggeduserid

于 2012-03-25T15:23:38.070 回答