2012-12-02 112 views
0

我已ASP.NET MVC 4個網站,其中的文章網址是:www.site.com/article/623/friendly-url-here友好的URL重定向

我wan't,如果有人訪問沒有文章友好的URL, 將完成沒有重定向和訪問數據庫的兩倍(這將使得網頁加載速度較慢)。

現在:

進入www.site.com/article/623 - >進入到www.site.com/article/623

,因爲我希望它是:

進入www.site.com/article/623/the-article-nice-url

(注意了 - 文章 - 漂亮的URL添加)

更新:

路線,因爲我在RouteConfig使用:

// Articles 
routes.MapRoute(
    name: "Articles", 
    url: "articles/{articleId}/{friendlyUrl}", 
    defaults: new { controller = "PageManagement", action = "ViewArticle", articleId = UrlParameter.Optional, friendlyUrl = UrlParameter.Optional } 
); 

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 

頁的元數據,因爲我要用到的數據與的DbContext存儲:

public class PageMetadata 
{ 
    [Key] 
    public int Id { get; set; } 

    public string RepresentationString { get; set; } 

    public DateTime CreationDate { get; set; } 

    public DateTime LastUpdateDate { get; set; } 

    public bool IsVisible { get; set; } 

    public string Title { get; set; } 

    public string Content { get; set; } 

    public string Link { get; set; } 
} 
+1

你的路由設置如何? – rickythefox

+0

更新了路由設置 –

回答

0

我終於用 「RedirectToRoute」,而將從數據庫中提取的數據存儲在會話對象中。

請求流:

  1. 請求只有在url ID。
  2. 從數據庫讀取網頁內容。
  3. 將獲取的頁面保存在以id(例如「article-53」)命名的會話對象中,並重定向到正確的路線。
  4. 如果存在,則從會話對象獲取頁面數據並清理會話對象。

    公衆的ActionResult ViewArticle(INT條款ArticleID,串friendlyUrl?) { 如果(條款ArticleID == NULL){ 回報 RedirectToActionPermanent( 「指數」, 「家」); }

     if (Session[string.Format("article-{0}", articleId)] != null) 
         { 
          PageMetadata desiredPage = (PageMetadata)Session[string.Format("article-{0}", articleId)]; 
          Session[string.Format("article-{0}", articleId)] = null; 
          return View(desiredPage); 
         } 
    
         var page = context.PageMetadatas.FirstOrDefault(p => p.Id == articleId); 
    
         if (page == null) 
         { 
          return RedirectToActionPermanent("Index", "Home"); 
         } 
    
         if (friendlyUrl != null) 
         { 
          // Redirect to proper name 
          if (friendlyUrl != page.RepresentationString) 
          { 
           Session[string.Format("article-{0}", page.Id)] = page; 
           return RedirectToRoute("Articles", new { controller = "PageManagement", action = "ViewArticle", articleId = articleId, friendlyUrl = page.RepresentationString }); 
          } 
          return View(page); 
         } 
         else 
         { 
          Session[string.Format("article-{0}", page.Id)] = page; 
          return RedirectToRoute("Articles", new { controller = "PageManagement", action = "ViewArticle", articleId = articleId, friendlyUrl = page.RepresentationString }); 
         } 
        } 
    
0

這個答案是不解決問題的方法,但是從不同的角度看:

你不能沒有重定向在瀏覽器中修改地址。

您應該確保,你暴露給用戶的只有一個版本鏈接。其次將重定向,並且沒有什麼錯,特別是當只有一種類型暴露時。這個問題的

地址就是很好的例子。如果你去http://stackoverflow.com/questions/13671002,你將被重定向到http://stackoverflow.com/questions/13671002/friendly-url-redirection

可以肯定的只有一個副本所使用的搜索引擎,你也應該把canoncal網址:​​

<link rel="canonical" href="http://stackoverflow.com/questions/13671002/friendly-url-redirection">

SEO不友好的地址應該更新以任何形式暴露出來。如果你不公開它,你將不會有雙重數據庫查詢。