Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to generate unique url id for each user
Posted
Comments
Sergey Alexandrovich Kryukov 22-Apr-14 9:13am    
Why it seems to be a problem for you? What have you tried so far?
—SA

1 solution

Each user will provide a unique ID used to identify this person. If some ID is busy, the user will be asked to choose another ID, until a new unique value is chosen.

One problem is: this ID can contain characters illegal for the name of the file system object or URL, so you will need to escape them. One of the simplest ways to do it is UrlEncode:
http://msdn.microsoft.com/en-us/library/zttxte6w%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx[^].

After UTL encoding, the names will remain unique. However, if you want to use some other method of escaping, escaped names may become non-unique. You need to choose the name for the URL at the moment of user registration. If you find that the URL is already taken, make it unique, say, by appending some integer number to it. Simply start with, say, 1, and increment this value until the resulting concatenated name becomes unique (you can develop algorithm with better performance, this is just the idea).

—SA
 
Share this answer
 
Comments
DamithSL 22-Apr-14 9:24am    
my 5! and how about this one?
public Uri GetURI()
{
string encoded = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
encoded = encoded
.Replace("/", "_")
.Replace("+", "-");
Uri baseUri = new Uri("http://www.yoursite.com");
Uri myUri = new Uri(baseUri, "?id="+encoded.Substring(0, 22));
return myUri;
}
Sergey Alexandrovich Kryukov 22-Apr-14 9:34am    
Thank you, Damith.
GUID can be used, but I would consider it as overkill.
GUID provide world uniqueness with high probability.
This task requires uniqueness in just one site, but with 100% probability (which you can also guarantee for GUIDs by an extra check).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900