我已经使用 GetRouteUrl 来创建 SEO 友好的 URL,但我现在想删除 %20 即空格并替换为破折号(“-”)。下面显示了我网站上的示例页面。我希望 Title 变量为“madonna-item”而不是“Madonna%20Item”。
/ProductsByDepartment/Gracya/Madonna/Madonna%20Item?CategoryId=9&productId=8&departmentId=4
我已经创建了一个类 (StringHelpers) 来修复 url 但我不知道在哪里调用 FixUrl
Public static class StringHelpers
{
public static string FixUrl(this string url)
{
string encodedUrl = (url ?? "").ToLower();
encodedUrl = Regex.Replace(encodedUrl, @"\+", "and");
encodedUrl = encodedUrl.Replace("'", "");
encodedUrl = Regex.Replace(encodedUrl, @"[^a-z0-9]", "-");
return encodedUrl;
}
包含更新链接的代码如下。我想在标题字段上使用 FixUrl,但这不起作用。
你能告诉我如何使用 FixUrl 吗?
<td class="Product_title" height="20px" width="180px">
<a href='<%#: GetRouteUrl("ProductExtraRoute", new {CategoryId = Eval("catId"), productId = Eval("productId"), departmentId = Eval("depId"), Title = Eval("Title")}).FixUrl()%>' class="Product">
<asp:Literal ID="literal2" runat="server" Text='<%# Eval(("Title").FixUrl()) %>'></asp:Literal>
最佳答案
试试 decodeURIComponent 检查这个http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp
在此之后用 - 替换所有规范
关于regex - 从 URL 中删除不需要的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24097300/