【发布时间】:2014-06-07 20:45:52
【问题描述】:
我已使用 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;
}
}
包含更新链接的代码如下。我想在 Title 字段上使用 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>
【问题讨论】:
标签: regex seo url-routing