【问题标题】:Add string to the requested URL将字符串添加到请求的 URL
【发布时间】:2014-01-08 20:25:35
【问题描述】:

我建立了一个路由,例如在访问时返回特定的博客条目 看起来是这样的

www.mywebsite.com/100

但是我希望在返回页面时我还将博客标题添加到 URL 例如用户可以输入上面的 url,但是当他返回页面时,url 必须如下所示:

www.mywebsite.com/100/My-Blog-Title

就像在 Stakoverflow 上发生的那样: 我输入这个网址

http://stackoverflow.com/questions/11227809/

收到请求后,URL 如下所示:

http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array

如何在我的 MVC 项目中实现这一点?谁能帮忙。

请同时评论 SEO 的友好性,同时建议您的首选方法。

谢谢

【问题讨论】:

标签: .net asp.net-mvc seo


【解决方案1】:
【解决方案2】:

您可以使用 JavaScript。

返回存储在某个 JavaScript 变量中的博客标题。

var blogTitle = "My-Blog-Title"

阅读博客标题并阅读此link 以更改网址而不进行重定向。

【讨论】:

    【解决方案3】:

    如果您在 Chrome 开发人员工具中打开“网络”选项卡并加载此问题 (http://stackoverflow.com/questions/21005768/) 的地址(仅限 ID),您会看到您收到了 HTTP 301 响应 - 永久重定向到友好的网址 (http://stackoverflow.com/questions/21005768/add-string-to-the-requested-url/)。

    要在 ASP.NET MVC 中对此进行响应,您需要执行以下操作:

    public ActionResult GetThingy(int id)
    {
        var title = GetTitle(id);
        return RedirectPermanent(Url.Action("GetThingy", new { id = id, title = title} );
    }
    

    这将重定向到控制器上的另一个操作,例如:

    public ActionResult GetThingy(int id, string title)
    {
        //do your thing here
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-14
      • 2021-10-24
      • 1970-01-01
      • 2017-06-26
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多