Creating Unique URLs in ASP.NET

On my website, I need to create unique URLs that the admin user will use to send it to a group of users. A unique URL is created whenever an administrator creates a new form. I understand that I can use guid to represent unique URLs, but I am looking for something shorter (hopefully about 4 characters, as this is easier to remember). How to create a unique URL in ASP.NET that will look like this:

http://mydomain.com/ABCD

I understand that some URL shortening websites (like bit.ly) do something like this with a very short unique URL. Is there an algorithm I can use?

+3
source share
4

-

public static  string GetRandomString (int length)  
{  
 string charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";  
 StringBuilder sb = new StringBuilder();  
 Random rnd = new Random();

 while ((length--) > 0)  
 sb.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);  

 return sb.ToString();  
}  

GetRandomString(4);
+1

GUID (, 4 8 , 4 8 .)

, , , . , , - ( 10, , ), .

0

I believe bit.ly performs a hash and then base64 encodes the result. You can do the same, although it will be more than 4 characters. Be sure to add code that processes hash conflicts. You can add 1, 2, 3 etc. when the first hash is used.

Another approach is to create a new table in the database. Each time you need a new URL, add a row to this table. You can use PK as the URL value. This will give you up to 10,000 unique values ​​using only four characters. Base64 is encoded even more.

0
source

Source: https://habr.com/ru/post/1792708/


All Articles