【问题标题】:How do I generate a Friendly URL in C#?如何在 C# 中生成友好 URL?
【发布时间】:2010-09-07 11:00:34
【问题描述】:

如何在 C# 中生成友好 URL?目前我简单地用下划线替换空格,但是我将如何生成像 Stack Overflow 这样的 URL?

例如如何转换:

如何在 C# 中生成友好 URL?

进入

如何在 C 中生成友好的 URL

【问题讨论】:

    标签: c# friendly-url


    【解决方案1】:

    这是一个简单的函数,可以将你的字符串转换为 Url,你只需要传递标题或字符串,它就会将它转换为用户友好的 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();
        }
    

    【讨论】:

      【解决方案2】:

      不过,Jeff 的解决方案有几处可以改进的地方。

      if (String.IsNullOrEmpty(title)) return "";
      

      恕我直言,不是测试这个的地方。如果函数传递了一个空字符串,那么无论如何都会出现严重错误。抛出错误或根本不响应。

      // remove any leading or trailing spaces left over
      … muuuch later:
      // remove trailing dash, if there is one
      

      两倍的工作。考虑到每个操作都会创建一个全新的字符串,这很糟糕,即使性能不是问题。

      // replace spaces with single dash
      title = Regex.Replace(title, @"\s+", "-");
      // if we end up with multiple dashes, collapse to single dash            
      title = Regex.Replace(title, @"\-{2,}", "-");
      

      同样,基本上是两倍的工作:首先,使用正则表达式一次替换多个空格。然后,再次使用正则表达式一次替换多个破折号。解析两个表达式,在内存中构造两个自动机,在字符串上迭代两次,创建两个字符串:所有这些操作都可以折叠成一个。

      在我的脑海中,没有任何测试,这将是一个等效的解决方案:

      // make it all lower case
      title = title.ToLower();
      // remove entities
      title = Regex.Replace(title, @"&\w+;", "");
      // remove anything that is not letters, numbers, dash, or space
      title = Regex.Replace(title, @"[^a-z0-9\-\s]", "");
      // replace spaces
      title = title.Replace(' ', '-');
      // collapse dashes
      title = Regex.Replace(title, @"-{2,}", "-");
      // trim excessive dashes at the beginning
      title = title.TrimStart(new [] {'-'});
      // if it's too long, clip it
      if (title.Length > 80)
          title = title.Substring(0, 79);
      // remove trailing dashes
      title = title.TrimEnd(new [] {'-'});
      return title;
      

      请注意,此方法尽可能使用字符串函数而不是正则表达式函数和字符函数而不是字符串函数。

      【讨论】:

      • 很好的答案谢谢,我还添加了 URL = Regex.Replace(URL, @"", "");将 HTML 标签删除为“my article here”将变为“my-barticleb-here”
      • 路径也不能以句点结尾。也不确定你为什么使用 new [] {'-'},而你可以只使用'-'。
      【解决方案3】:

      我们是这样做的。请注意,边缘条件可能比您乍一看还多。

      if (String.IsNullOrEmpty(title)) return "";
      
      // remove entities
      title = Regex.Replace(title, @"&\w+;", "");
      // remove anything that is not letters, numbers, dash, or space
      title = Regex.Replace(title, @"[^A-Za-z0-9\-\s]", "");
      // remove any leading or trailing spaces left over
      title = title.Trim();
      // replace spaces with single dash
      title = Regex.Replace(title, @"\s+", "-");
      // if we end up with multiple dashes, collapse to single dash            
      title = Regex.Replace(title, @"\-{2,}", "-");
      // make it all lower case
      title = title.ToLower();
      // if it's too long, clip it
      if (title.Length > 80)
          title = title.Substring(0, 79);
      // remove trailing dash, if there is one
      if (title.EndsWith("-"))
          title = title.Substring(0, title.Length - 1);
      return title;
      

      【讨论】:

      【解决方案4】:

      这就是其中的一部分(使用有效字符的白名单):

      new Regex("[^a-zA-Z-_]").Replace(s, "-")
      

      但是,它确实会为您提供一个以“--”结尾的字符串。所以也许第二个正则表达式从字符串的开头/结尾修剪那些,并可能将任何内部“--”替换为“-”。

      【讨论】:

        猜你喜欢
        • 2010-11-30
        • 2010-09-07
        • 1970-01-01
        • 2010-11-07
        • 2016-01-13
        • 2013-01-31
        • 1970-01-01
        • 2013-06-10
        相关资源
        最近更新 更多