Pass id but don’t show it in the actual url

I am trying to get friendly urls. The friendly URL at the moment is this:

local / client / 1 / namehere

I want them to be like this:

local / client / namehere

But still get the want identifier (because I'm looking for my database with this id). What are my options? And is it possible?

This is my MapRoute:

context.MapRoute(
            "Customer_Default",
            "customer/{customername}/",
            new { controller = "customer", action = "view", id = "", customername = "" }
        );

Here is how I connect:

@Html.ActionLink(c.Bedrijfsnaam, "view", "customer", new { id = c.Klantnummer, customername = UrlEncoder.ToFriendlyUrl(c.Bedrijfsnaam) }, null)

Thank.

Update: Oh, it doesn't matter if the user changes it. I just want it not to show. So the user can easily change the URL to where he wants to go. And no need to worry about ids. (But I still need it. :))

+5
3

, id .

, . , . "" , .

, ( http post http get ). : , http get, . , db . , , .

+2

, , id + . , .

id , AES DES. id ( 64-256 ), id ( .. !)

// Generate key. You do it once and save the key in the web.config or in the code
var encryptorForGenerateKey = Aes.Create();
encryptorForGenerateKey.BlockSize = 128;
encryptorForGenerateKey.KeySize = 128;
encryptorForGenerateKey.GenerateKey();
encryptorForGenerateKey.GenerateIV();

var key = encryptorForGenerateKey.Key;
var iv = encryptorForGenerateKey.IV;

// Encrypt

var encryptor = Aes.Create();
var encryptorTransformer = encryptorForGenerateKey.CreateEncryptor(key, iv);

int id = 123;
var bytes = BitConverter.GetBytes(id);
var encrypted = encryptorTransformer.TransformFinalBlock(bytes, 0, bytes.Length);
var encryptedString = BitConverter.ToString(encrypted);

Console.WriteLine(encryptedString);

// Decrypt

var decryptor = Aes.Create();
var decryptorTransformer = decryptor.CreateDecryptor(key, iv);

String[] arr = encryptedString.Split('-');
byte[] encrypted2 = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
    encrypted2[i] = Convert.ToByte(arr[i], 16);
}

// If the block is irregular there is the possibility TransformFinalBlock will throw

var result = decryptorTransformer.TransformFinalBlock(encrypted2, 0, encrypted2.Length);

if (result.Length != sizeof(int))
{
    throw new Exception();
}

var id2 = BitConverter.ToInt32(result, 0);

id= 123 id= 4E-CD-80-9E-7E-FB-A7-B9-74-B6-3A-37-57-9C-BD-A9. , Base64 -, . -: D2B4F51E6577967A2262E3AE51F3EC74, Base64: 0rT1HmV3lnoiYuOuUfPsdA==

, , DES . DES id :

F2-54-4B-CE-23-83-96-C2 // With -
F2544BCE238396C2 // Without -
8lRLziODlsI= // Base64

DES, AES DES BlockSize KeySize.

+2

If you want the result to be deterministic, you need to pass a unique combination of parameters. In your case, you can use the "customername" parameter with some number after it for customers with the same name (you will need to save this number in your db along with the customer name). For instance:

localhost/customer/Aaron_Babichev - for the first customer with name Aaron Babichev
localhost/customer/Aaron_Babichev_2 - for the second customer with name Aaron Babichev
...

Your route mask will look like customer/{customername}_{customerIndex}/.

0
source

All Articles