【问题标题】:Rewrite Url in ASP .NET MVC 3 - add *.html suffix to links在 ASP .NET MVC 3 中重写 Url - 将 *.html 后缀添加到链接
【发布时间】:2012-02-21 14:19:08
【问题描述】:

我正在创建简单的应用程序,某种投资组合。我听说最好在链接中添加 *.html 后缀,因为它可以让我在 Google 索引时获得更好的 SEO 结果...

无论如何,有没有办法修改默认路由/重写 url,使我的链接看起来像这样(我使用的是波兰语,它们对我的访问者来说是可读的):

domain.pl/index.html
domain.pl/kontakt.html
domain.pl/oferta.html
domain.pl/sklepy.html

这些链接被翻译成一个控制器(如 HomeController),但来自 {0}.html 链接的 {0} 将用作操作名称?或者更好的是,我想将 {0} 从 Url 映射到英文动作名称,例如:

index.html = index action
kontakt.html = contact action
oferta.html = offer action
sklepy.html = shops action

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 iis-7 url-rewriting


    【解决方案1】:

    不确定是否有更好的 SEO 结果,但添加后缀很简单

            routes.MapRoute(
                "Default",
                "{action}.html",
                new { controller = "Home", action = "Index" }
            );
    

    只需将 .html 后缀添加到操作参数占位符即可。

    对于翻译,您可以使用 ActionNameAttribute

        [ActionName("kontakt")]
        public ActionResult Contact()
        {
            return View();
        }
    

    结合以上两个代码,您可以将domain.pl/kontakt.html 映射到Home/Contact 操作。

    【讨论】:

      【解决方案2】:

      对于翻译和后缀,您可以尝试使用AttributeRouting。 安装此软件包后,您无需在 Global.asax 中配置路由,控制器将如下所示:

      [GET("index.html")]
      public ActionResult Index()
      {
          return View();
      }
      
      [GET("/any/url/path/kontakt.html")]
      public ActionResult Contact()
      {
          return View();
      }
      
      [GET("oferta.html")]
      public ActionResult Offer()
      {
          return View();
      }
      

      顺便说一句,如果您想删除每个属性上重复的 .html,您可以创建自己的扩展 GETAttribute 的属性并附加 .html。如果您有很多页面要配置,这将很有用。

      【讨论】:

        猜你喜欢
        • 2020-10-03
        • 2015-11-30
        • 1970-01-01
        • 2015-10-03
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        相关资源
        最近更新 更多