【问题标题】:Asp.Net Routing - Replace white spaces %20 with dashes from URLs generated [duplicate]Asp.Net Routing - 用生成的 URL 中的破折号替换空格 %20 [重复]
【发布时间】:2011-10-22 03:03:51
【问题描述】:

可能重复:
How does StackOverflow generate its SEO-friendly URLs?

我使用 Asp.net 4 和路由

我使用此路由为我的网站创建 SEO 友好的 URL。 Title url 参数是一个字符串示例“这是一个标题”,结果我在浏览器中以这种格式/Content/This%20is%20a%20Title 获取 URL。

我宁愿用更易读的破折号替换空格 %20,例如:/Content/This-is-a-Title

知道怎么做吗?感谢您对此的帮助

        routes.MapPageRoute(
            "View Content",                     // Route name
            "Content/{Title}",                  // Route URL
            "~/Cms/FrontEndCms/Content.aspx"    // Web page to handle route
        );

【问题讨论】:

    标签: c# asp.net routing seo


    【解决方案1】:

    当您重定向到此 URL 时,请使用以下内容:

    Response.Redirect(Page.GetRouteUrl(
        "View Content", 
        new { Title=(yourtitlehere).ToString().Trim().Replace(" ","-") })
    );
    

    我添加了Trim()。如果标题以空格开头或结尾,服务器可能会认为它是不同的路径并返回404: Resource not found

    【讨论】:

    • 您好,感谢您的回答,但代码应该在本机路由上运行。
    • 是的,当您重定向时,或者您可以使用该代码设置链接的网址
    • 谢谢,现在我明白了。您的解决方案很快就会被采用,但我想可能会导致一些性能下降。最后我剥离了我的 url 并存储在数据库中,所以我使用路由来获取干净的 URL 或我的链接。无论如何,非常感谢:)
    • 我遇到了同样的问题,来到了这个链接。我认为它很有用:deliveron.com/blog/post/…
    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2011-01-18
    • 2011-03-08
    • 1970-01-01
    • 2013-07-08
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多