【问题标题】:ASP.NET MVC - Wordpress Style URLsASP.NET MVC - Wordpress 样式 URL
【发布时间】:2010-06-21 16:07:33
【问题描述】:

Wordpress 备受推崇,并被证明遵循良好做法,这反过来又有助于在搜索引擎中排名。

一个经过验证的因素是 seo 友好的网址。比如下面的例子;

www.myblog.com/test
www.myblog.com/another
www.myblog.com/contact

假设我们有两个控制器,每个控制器都有他们获胜的动作;

View/BlogPost
View/BlogCategory
Contact/Form
Contact/Post

wordpress 视图引擎足够灵活,测试 url 可以解析为 BlogPost,而另一个 url 可以解析为 BlogCategory。

有谁知道这在 MVC 中是否可行,是否可行?我正在考虑一个自定义的 404 处理程序,它将保留浏览器中的 url,然后触发所需的操作。

【问题讨论】:

    标签: asp.net asp.net-mvc seo


    【解决方案1】:

    你像这样在 global.asax 中定义一个路由,底部的路由是默认的:

            routes.MapRoute(
                "Test",
                "test",
                new { controller = "View", action = "BlogPost"}
            );
    
            routes.MapRoute(
                "Another",
                "another",
                new { controller = "View", action = "BlogCategory"}
            );
    
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    

    MapRoute() 中的第二个参数是 URL,在您的例子中是“test”和“another”。您可能可以在这里看到,您可以将此路由数据存储在其他位置,例如数据库,然后通过它们进行 foreach,将每个数据添加到 RouteCollection。

    【讨论】:

      【解决方案2】:

      您可以通过RedirectToAction() 调用向控制器添加一个操作

      ...
      public ActionResult test()
      {
          return RedirectToAction("BlogPost");
      }
      ..
      public ActionResult another()
      {
          return RedirectToAction("BlogCategory");
      }
      ...
      

      【讨论】:

      • Wordpress 是一个自发布的 cms,有没有办法根据动态变量做出这个决定?
      • 我相信您可以在 Global.asax 中使用自己的 Route 来做到这一点——但这是 ASP.NET MVC 的一个领域,我还没有进入。
      【解决方案3】:

      正如其他人在上面回答的那样,拥有这样的路线很容易拥有友好的“Slug”网址。还有一些技巧可以构建非常干净的 URL,例如 WordPress。我写了两篇关于这个主题的博文,可以帮助你更好地理解 Slug URL:Build pretty clean URL for your dynamic pages using JavaScriptTranslating your content title using Google Translate API to use in a URL

      【讨论】:

        猜你喜欢
        • 2011-02-01
        • 2010-12-23
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2011-09-11
        • 2018-11-01
        相关资源
        最近更新 更多