<p>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?</p>

<p>For example how can I convert:</p>

<blockquote>
  <p>How do I generate a Friendly URL in C#?</p>
</blockquote>

<p>Into</p>

<blockquote>
  <p>how-do-i-generate-a-friendly-url-in-C</p>
</blockquote>

[{'answer_id': 37817, 'author': 'Jeff Atwood', 'author_id': 1, 'author_profile': 'https://Stackoverflow.com/users/1', 'pm_score': 4, 'selected': False, 'text': '<p>Here\'s how we do it. Note that there are probably more edge conditions than you realize at first glance..</p>\n\n<pre><code>if (String.IsNullOrEmpty(title)) return "";\n\n// remove entities\ntitle = Regex.Replace(title, @"&amp;\\w+;", "");\n// remove anything that is not letters, numbers, dash, or space\ntitle = Regex.Replace(title, @"[^A-Za-z0-9\\-\\s]", "");\n// remove any leading or trailing spaces left over\ntitle = title.Trim();\n// replace spaces with single dash\ntitle = Regex.Replace(title, @"\\s+", "-");\n// if we end up with multiple dashes, collapse to single dash            \ntitle = Regex.Replace(title, @"\\-{2,}", "-");\n// make it all lower case\ntitle = title.ToLower();\n// if it\'s too long, clip it\nif (title.Length &gt; 80)\n    title = title.Substring(0, 79);\n// remove trailing dash, if there is one\nif (title.EndsWith("-"))\n    title = title.Substring(0, title.Length - 1);\nreturn title;\n</code></pre>\n'}, {'answer_id': 37818, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 3, 'selected': False, 'text': '<p>This gets part of the way there (using a whitelist of valid characters):</p>\n\n<pre><code>new Regex("[^a-zA-Z-_]").Replace(s, "-")\n</code></pre>\n\n<p>It does, however, give you a string that ends with "--". So perhaps a second regex to trim those from the beginning/end of the string, and maybe replace any internal "--" to "-".</p>\n'}, {'answer_id': 37880, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 7, 'selected': True, 'text': '<p>There are several things that could be improved in Jeff\'s solution, though.</p>\n\n<pre><code>if (String.IsNullOrEmpty(title)) return "";\n</code></pre>\n\n<p>IMHO, not the place to test this. If the function gets passed an empty string, something went seriously wrong anyway. Throw an error or don\'t react at all.</p>\n\n<pre><code>// remove any leading or trailing spaces left over\n… muuuch later:\n// remove trailing dash, if there is one\n</code></pre>\n\n<p>Twice the work. Considering that each operation creates a whole new string, this is bad, even if performance is not an issue.</p>\n\n<pre><code>// replace spaces with single dash\ntitle = Regex.Replace(title, @"\\s+", "-");\n// if we end up with multiple dashes, collapse to single dash            \ntitle = Regex.Replace(title, @"\\-{2,}", "-");\n</code></pre>\n\n<p>Again, basically twice the work: First, use regex to replace multiple spaces at once. Then, use regex again to replace multiple dashes at once. Two expressions to parse, two automata to construct in memory, iterate twice over the string, create two strings: All these operations can be collapsed to a single one.</p>\n\n<p>Off the top of my head, without any testing whatsoever, this would be an equivalent solution:</p>\n\n<pre><code>// make it all lower case\ntitle = title.ToLower();\n// remove entities\ntitle = Regex.Replace(title, @"&amp;\\w+;", "");\n// remove anything that is not letters, numbers, dash, or space\ntitle = Regex.Replace(title, @"[^a-z0-9\\-\\s]", "");\n// replace spaces\ntitle = title.Replace(\' \', \'-\');\n// collapse dashes\ntitle = Regex.Replace(title, @"-{2,}", "-");\n// trim excessive dashes at the beginning\ntitle = title.TrimStart(new [] {\'-\'});\n// if it\'s too long, clip it\nif (title.Length &gt; 80)\n    title = title.Substring(0, 79);\n// remove trailing dashes\ntitle = title.TrimEnd(new [] {\'-\'});\nreturn title;\n</code></pre>\n\n<p>Notice that this method uses string functions instead of regex functions and char functions instead of string functions whenever possible.</p>\n'}, {'answer_id': 58835281, 'author': 'Arslan Ali', 'author_id': 3492230, 'author_profile': 'https://Stackoverflow.com/users/3492230', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>    public static string GenerateUrl(string Url)\n    {\n        string UrlPeplaceSpecialWords = Regex.Replace(Url, @"&amp;quot;|[\'"",&amp;?%\\.!()@$^_+=*:#/\\\\-]", " ").Trim();\n        string RemoveMutipleSpaces = Regex.Replace(UrlPeplaceSpecialWords, @"\\s+", " ");\n        string ReplaceDashes = RemoveMutipleSpaces.Replace(" ", "-");\n        string DuplicateDashesRemove = ReplaceDashes.Replace("--", "-");\n        return DuplicateDashesRemove.ToLower();\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/37809', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']