如何编写包含用户名的路由

时间:2011-08-19 19:23:23

标签: c# asp.net-mvc model-view-controller

我正在构建一个asp.net mvc网站,在用户登录后他可以访问他的个人资料部分页面,目前这些页面的URL就像是www.example.com/profile,我想要的是制作这样的URL www.example.com/USERNAME

如何在用户登录时编写可在个人资料页面中使用的此路线?

更新

基于下面的答案,我这样写了:

routes.MapRoute(
                "AccountSettins",
                   "AccountSettings",
               new { controller = "AccountSettings", action = "Index" }
            );

            routes.MapRoute(
                 "myRouteName",
                    "{username}",
                new { controller = "Profile", action = "Index" }
             );


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

和控制器:

[Authorize]
    public class ProfileController : BaseController
    {
        //
        // GET: /Profile/

        public ActionResult Index(string username= "")
        { ...

但是现在用户登录后,他的用户名是“xyz”,他可以访问www.example.com/xyz,这将导致个人资料页面,但如果他还写了网址www.example.com/ abc他会正常访问相同的个人资料页面,从用户的角度来看这有什么奇怪的,如何解决这个问题呢?

5 个答案:

答案 0 :(得分:2)

在你的Global.asax ......

routes.MapRouteWithName(
    "routeUserProfile",
    "{username}",
    new { controller = "User", action = "Profile", username = "" });

在您的用户控制器....

public ActionResult Profile(string username) {
    //conditional logic to check if username is user
    // render user profile with special user-only stuff
    //else
    // render only generic stuff about user
}

答案 1 :(得分:1)

routes.MapRoute(
    "myRouteName",
    "{username}",
    new { controller = "Home", action = "Profile" }
    );

您可以指定所需的控制器和操作,只需使用参数的用户名作为Home类的方法配置文件。

答案 2 :(得分:1)

您需要专门为此编写一个控制器并创建一个路径,如:

routes.MapRoute(
  "UserPage", // Route name
  "{username}", // URL with parameters
  new { controller = "User", action = "Index", username = ""} // Parameter defaults
  );

请点击此处了解更多详情: http://dotnet.dzone.com/articles/aspnet-mvc-routing-basics?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone

答案 3 :(得分:1)

在global.asax文件中添加以下路由

        routes.MapRoute(
            "UsersRoute", // Route name
            "{username}", // URL with parameters
            new { controller = "Test", action = "Index", username = "" } 
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );

根据第一条路线,将以下控制器添加为

public class TestController : Controller
{       
    public ActionResult Index(string username )
    {
        var p = username;
        return View();
    }
}

答案 4 :(得分:1)

要阻止用户查看其他个人资料,只需检查他/她是否可以执行此操作。

public ViewResult Index(string username)
{
  if (CanSeeOthersProfiles(username)) //your function to check currently logged user and his privileges
  {
     var model = new MyModel();
     //do your logic
     return View(model);
  }
  else
     return RedirectToAction("index", "home");
}