开发者

Unicode & International slug names for use within an MVC URI?

开发者 https://www.devze.com 2023-02-12 06:09 出处:网络
I\'m looking at the MSFT Patterns开发者_高级运维 and Practices guide for MVC on Azure and they have code similar to the following:

I'm looking at the MSFT Patterns开发者_高级运维 and Practices guide for MVC on Azure and they have code similar to the following:

    public static string GenerateSlug(this string txt, int maxLength)
    {
        string str = RemoveAccent(txt).ToLower();

        str = Regex.Replace(str, @"[^a-z0-9\s-]", string.Empty);
        str = Regex.Replace(str, @"\s+", " ").Trim();
        str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
        str = Regex.Replace(str, @"\s", "-");

        return str;
    }

    public static string RemoveAccent(this string txt)
    {
        byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
        return System.Text.Encoding.ASCII.GetString(bytes);
    } 

What changes will I have to make to support Eastern and non-english languages, and still keep the SEO benefit of the slug?


You may take a look at how the slugs are generated on StackOverflow.

0

精彩评论

暂无评论...
验证码 换一张
取 消