Cleaning up the string is relatively straightforward. Something like this should work:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
public static class LinkHelper
{
private static readonly Regex ExtraHyphen = new Regex("\\-{2,}", RegexOptions.Compiled);
private static readonly HashSet<char> IllegalChars = new HashSet<char>
{
':', '/', '?', '#', '[', ']', '@', '*', '.', ',', '"', '\'',
};
private static string RemoveExtraHyphen(string text)
{
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
return ExtraHyphen.Replace(text, "-");
}
private static string RemoveDiacritics(string text)
{
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
string normalized = text.Normalize(NormalizationForm.FormD);
return new string(normalized
.Where(c => UnicodeCategory.NonSpacingMark != CharUnicodeInfo.GetUnicodeCategory(c))
.ToArray());
}
public static string UrlSlug(string text)
{
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
string slug = new string(text
.Where(c => !IllegalChars.Contains(c))
.Select(c => ' ' == c ? '-' : c)
.ToArray());
slug = slug.Replace("&", "-and-");
slug = RemoveDiacritics(slug);
slug = RemoveExtraHyphen(slug);
return HttpUtility.UrlEncode(slug).Replace("%", string.Empty);
}
}
<asp:HyperLink ID="lbstatepincode" runat="server"
NavigateUrl='<%# "~/" + LinkHelper.UrlSlug(Eval("state_name_without_pin", "{0}")) %>'
Text='<%#Bind("state_name")%>'
/>
How you then map that "cleaned-up" URL back to the correct state is more complicated, and depends on your URL routing strategy.