我正在使用asp.net mvc作为我的网站项目。我觉得我的路线有错误,但我不确定是不是错了。我会解释一下情况。 我正在缓存中使用生成的密钥
缓存我的操作结果(html输出) 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();
}
ex:对于HomePage,生成的缓存键是P-Controller = Home.Action = Index和
我的网站管理员也有像LoginBox一样的子进程(在MembershipController / LoginBox中) 它的缓存键是C-Controller = Membership.Action = LoginBox。
到目前为止,一切都很好。
我的网站中也有子类别 域/类别1 域/类别1 /子类别1 域/类别1 / subcategory2 域/类别2
当我从domain / category1浏览子类别时 我生成的密钥失败,因为我的路由错误
filterContext.RouteData.Values: Controller =会员资格 Action = LoginBox ctg1 = category1 ctg2 =“” ctg3 =“”
为什么这些是混合的。它正在使用“类别”路由映射,但我认为它必须使用“默认”路由映射。
我的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方法是从db检查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;
}
}
答案 0 :(得分:0)
希望这对你有帮助,它会告诉你网址匹配的路线,我对这个问题感到有些困惑。
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx