Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Route to Username

Tags:

I am trying to create a route with a Username...

So the URL would be mydomain.com/abrudtkhul (abrudtkhul being the username)

My application will have public profiles based on usernames (Ex: http://delicious.com/abrudtkuhl). I want to replicate this URL scheme.

How can I structure this in ASP.Net MVC? I am using Membership/Roles Providers too.

like image 245
Andy Brudtkuhl Avatar asked Oct 24 '08 20:10

Andy Brudtkuhl


2 Answers

Here's what you want to do, first define your route map:

routes.MapRoute(
             "Users",
              "{username}", 
              new { controller = "User", action="index", username=""});

What this allows you to do is to setup the following convention:

  • Controller: User (the UserController type)
  • Action: Index (this is mapped to the Index method of UserController)
  • Username: This is the parameter for the Index method

So when you request the url http://mydomain.com/javier this will be translated to the call for UserController.Index(string username) where username is set to the value of javier.

Now since you're planning on using the MembershipProvider classes, you want to something more like this:

 public ActionResult Index(MembershipUser usr)
 {
    ViewData["Welcome"] = "Viewing " + usr.UserName;

    return View();
 }

In order to do this, you will need to use a ModelBinder to do the work of, well, binding from a username to a MembershipUser type. To do this, you will need to create your own ModelBinder type and apply it to the user parameter of the Index method. Your class can look something like this:

public class UserBinder : IModelBinder
{
   public ModelBinderResult BindModel(ModelBindingContext bindingContext)
   {
      var request = bindingContext.HttpContext.Request;
      var username = request["username"];
      MembershipUser user = Membership.GetUser(username);

      return new ModelBinderResult(user);
   }
}

This allows you to change the declaration of the Index method to be:

public ActionResult Index([ModelBinder(typeof(UserBinder))] 
    MembershipUser usr)
{
    ViewData["Welcome"] = "Viewing " + usr.Username;
    return View();
}

As you can see, we've applied the [ModelBinder(typeof(UserBinder))] attribute to the method's parameter. This means that before your method is called the logic of your UserBinder type will be called so by the time the method gets called you will have a valid instance of your MembershipUser type.

like image 128
Javier Lozano Avatar answered Oct 20 '22 14:10

Javier Lozano


You might want to consider not allowing usernames of certain types if you want to have some other functional controllers like Account, Admin, Profile, Settings, etc. Also you might want your static content not to trigger the "username" route. In order to achieve that kind of functionality (similar to how twitter urls are processed) you could use the following Routes:

// do not route the following
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("content/{*pathInfo}"); 
routes.IgnoreRoute("images/{*pathInfo}");

// route the following based on the controller constraints
routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    , new { controller = @"(admin|help|profile|settings)" } // Constraints
);

// this will catch the remaining allowed usernames
routes.MapRoute(
    "Users",
    "{username}",
    new { controller = "Users", action = "View", username = "" }
);

Then you will need to have a controller for each of the tokens in the constraint string (e.g. admin, help, profile, settings), as well as a controller named Users, and of course the default controller of Home in this example.

If you have a lot of usernames you don't want to allow, then you might consider a more dynamic approach by creating a custom route handler.

like image 32
Steve T Avatar answered Oct 20 '22 15:10

Steve T