开发者

how to write a route which include the username

I am building an asp.ne开发者_开发技巧t mvc website, after the user login he can access his profile section pages and currently these pages URL is like that www.example.com/profile , what I want is to make the URL like that www.example.com/USERNAME

How to write this route which will work just in profile page when the user login?

Update:

based on the answers below, I wrote it like this:

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

and the controller:

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

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

But now after the user login and his user name was "xyz" he can go to www.example.com/xyz and this will lead to the profile page, but if he also wrote the url www.example.com/abc he will go to the same profile page normally which is something strange from the user point of view, how to solve this issue?


In your Global.asax...

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

In your User controller....

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
}


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

You can specify you controller and action you want and just use the username for your parameter for the method Profile of the Home class.


You will need to write a controller specifically for this and create a route like:

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

See here for more details: http://dotnet.dzone.com/articles/aspnet-mvc-routing-basics?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone)


In the global.asax file add the following routes

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

And according to the first route add the following controller as bellow

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


To prevent user to see others profile, just check in the action if he/she can do that.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜