2010-12-06 133 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; 
    } 


} 

回答