2012-03-25 82 views
1

我正在构建一个小型的MVC应用程序。之后用户登录我想他/她显示路线:使用用户名MVC MapRoute用户名

www.appname.com/username/

底下当然同样的动作被称为为每个用户如/home/index。我如何编写我的MapRoute来实现这个目标?我应该使用哪些其他代码(属性)?

+0

什么字符在用户名中被授权?例如,你允许用户在你的网站上注册username = Home/Index吗? – 2012-03-25 15:42:36

+0

@Darin Dimitrov,用户名称将不会有特殊字符。我主要是为了学习目的而构建它,所以没有太复杂的东西,只想让用户登录并看到可以与其他人共享的自定义网址。 – mishap 2012-03-25 16:21:13

回答

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