【问题标题】:Remove unwanted characters from URL从 URL 中删除不需要的字符
【发布时间】: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


    【解决方案1】:

    尝试 decodeURIComponent 检查这个http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

    在此之后将所有规格替换为 -

    【讨论】:

    • 意见:您不应该使用或链接到w3schools。它不是可靠的信息来源,我们不想鼓励使用它。我建议改用Mozilla Developer Network
    • 请您告诉我在上面的示例中如何使用 decodeURIComponent ? javascript 代码是否被添加到 .aspx 代码中,以便在运行时修改 url?
    猜你喜欢
    • 2011-12-24
    • 2018-08-08
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多