0

I want to setup the following url in my MVC4 website, using the user's full name in the url:

http://www.myapp.com/profile/steve-jones

I have setup the following route in Global.asax:

    routeCollection.MapRoute(
      "profile", "profile/{userName}", 
       new { controller = "myController", action = "profile", userName = string.Empty 
    });

And I can take the parameter 'steve-jones' and match it to a user with matching name. My only problem though is, what if there is more than one 'Steve Jones', how can I handle this?

Does anyone know of a workaround/solution to this so that I can use a user's full name as part of the url and still be able to retrieve the correct user in the controller method?

Am I forced into including the user's id with the url (something that I do not want to appear)?

DevDave
  • 6,700
  • 12
  • 65
  • 99

1 Answers1

2

The usual way of handling this is by appending a number when creating the profiles. So if "steve-jones" is already a name in the database, then make the user's display name "steve-jones2". You basically have to insist that all profile urls are unique, which includes updating any existing database and account creation code.

Alternatively (and/or additionally), if two same names are found then have the script reroute to a disambiguation page where the user is presented with links and snippet of profile info of the many existing Steve Joneseses so they can go to the full correct profile.

Another way of handling it is by giving all user profiles an additional numeric code on the end. At my university all logins are based on name, so they give everyone pseudo-random 3-digit extensions so that they are safe as long as they don't get 1000 people with the exact same names :)

Some people might be happier being steve-jones-342 if there is no steve-jones or steve-jones1, if you're concerned.

BrianH
  • 2,140
  • 14
  • 20
  • thanks Brian, very helpful. The only problem is that there are no user names just first and last names, and I don't have access to the db, its being handled by CRM – DevDave Mar 21 '13 at 16:53
  • 1
    I see, then you'd have to do it on the fly. Perhaps if there was an account creation date you could sort by that and assign numbers that way - so the first steve would always be steve1, etc. This way you could have an effective meta-variable that you don't have to alter the database for. – BrianH Mar 21 '13 at 17:03