How do I generate a Friendly URL in C#?

前端 未结 4 1677
生来不讨喜
生来不讨喜 2020-12-04 10:10

How can I go about generating a Friendly URL in C#? Currently I simple replace spaces with an underscore, but how would I go about generating URL\'s like Stack Overflow?

4条回答
  •  再見小時候
    2020-12-04 11:02

    here is a simple function which can convert your string to Url, you just need to pass title or string it will convert it to user friendly Url.

        public static string GenerateUrl(string Url)
        {
            string UrlPeplaceSpecialWords = Regex.Replace(Url, @""|['"",&?%\.!()@$^_+=*:#/\\-]", " ").Trim();
            string RemoveMutipleSpaces = Regex.Replace(UrlPeplaceSpecialWords, @"\s+", " ");
            string ReplaceDashes = RemoveMutipleSpaces.Replace(" ", "-");
            string DuplicateDashesRemove = ReplaceDashes.Replace("--", "-");
            return DuplicateDashesRemove.ToLower();
        }
    

提交回复
热议问题