Create URL Friendly Strings (ex: convert Montreal to Montreal)

I am writing a web application that requires friendly URLs, but I'm not sure how to handle non-7-bit ASCII characters. I do not want to replace characters with emphasis on URL encoded objects. Is there a C # method that allows such a conversion, or do I need to actually display every single case that I want to handle?

+3
source share
7 answers

I donโ€™t know how to do this in C #, but the magic words you want are "Unicode decomposition." There's a standard way to break up composed characters such as "รฉ", and then you just need to filter out non-ASCII files.

Edit: this may be what you are looking for.

+3
source

Use UTF-8:

Non-ASCII characters must first be encoded in accordance with UTF-8 [STD63], and then each octet of the corresponding UTF-8 sequence must be percentage encoded to represent URI characters. - RFC 3986

+2
source

- : URL-: URL-

, . . .

+1

: http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx

private string LatinToAscii(string InString)
{
string newString = string.Empty, charString;
char ch;
int charsCopied;

for (int i = 0; i < InString.Length; i++)
{
    charString = InString.Substring(i, 1);
    charString = charString.Normalize(NormalizationForm.FormKD);
    // If the character doesn't decompose, leave it as-is

    if (charString.Length == 1)
        newString += charString;
    else
    {
        charsCopied = 0;
        for (int j = 0; j < charString.Length; j++)
        {
            ch = charString[j];
            // If the char is 7-bit ASCII, add

            if (ch < 128)
            {
                newString += ch;
                charsCopied++;
            }
        }
        /* If we've decomposed non-ASCII, give it back
         * in its entirety, since we only mean to decompose
         * Latin chars.
        */
        if (charsCopied == 0)
            newString += InString.Substring(i, 1);
    }
}
return newString;
}
+1

- . . , . , , , , querystring, ? .

/ , , querystring. , - - , .. , , , . 2005 , , , id querystring. , . , , , . , - AJAX, .

- , - , , - , , .

+1

, , , , Replace() string.

0
0

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


All Articles