2010-12-06 130 views
0

我使用asp.net mvc作爲我的網站項目。我認爲我在routedata中有錯誤的東西,但我不確定它是錯誤的還是OK的。我將解釋這種情況。 我緩存我的行動結果(HTML輸出)的緩存與生成的密鑰MVC RouteDatas很困惑

 public static string GetKeyFromActionExecutingContext(ControllerContext filterContext) 
    { 
     StringBuilder keyBuilder = new StringBuilder(); 

     if (filterContext.IsChildAction) 
      keyBuilder.Append("C-"); 
     else 
      keyBuilder.Append("P-"); 

     foreach (var item in filterContext.RouteData.Values) 
     { 
      keyBuilder.AppendFormat("{0}={1}.", item.Key, item.Value); 
     } 

     return keyBuilder.ToString(); 

    } 

例如:對於主頁,產生的高速緩存關鍵是P-控制器= Home.Action =指數和

我也有我的網站管理員中的孩子像LoginBox(它在MembershipController/LoginBox中) 它的緩存鍵是C-Controller = Membership.Action = LoginBox。

一切都是okey到現在。

我也曾經在我的網站子類別像

,當我瀏覽從域/組別 子類別我產生 域/組別 域/類別1 /子類別1 域/組別/ subcategory2 域/產品組別密鑰是失敗了,因爲我的routedatas是錯誤的

filterContext.RouteData.Values: 控制器=會員 行動= LoginBox ctg1 =組別 ctg2 =「」 ctg3 =「」

爲什麼這些是混合的。它使用「Category」routemapping,但我認爲它必須使用「Default」routemapping。

我的Global.asax像下面

routes.MapRoute(
      "Category", 
      "{ctg0}/{ctg1}/{ctg2}/{ctg3}", 
     new 
     { 
      controller = "Category", 
      action = "Index", 
      ctg0 = "", 
      ctg1 = "", 
      ctg2 = "", 
      ctg3 = "" 
     }, 
     new 
     { 
      ctg0 = new CategoryRouteConstraint(), 
     } 
     ); 

routes.MapRoute(
       "Default",            
       "{controller}/{action}/{id}",           new { controller = "Home", action = "Index", id = "" }, 
       new { controller = @"[^\.]*" }       
      ); 

而且我CategoryRouteConstraint方法是從數據庫是ctg0值檢查是一個類別名稱

public class CategoryRouteConstraint : IRouteConstraint 
{ 

    public Boolean Match(
     HttpContextBase httpContext, 
     Route route, 
     String sParameterName, 
     RouteValueDictionary values, 
     RouteDirection routeDirection 
     ) 
    { 
     if ((routeDirection == RouteDirection.IncomingRequest)) 
     { 
      if (values["ctg0"] != null && !string.IsNullOrEmpty(values["ctg0"].ToString())) 
       return Category.IsRoutingForCategory(values["ctg0"].ToString()); 
      return false; 
     } 
     return false; 
    } 


} 

回答