MVC RouteDatas 很混乱
我正在为我的网站项目使用 asp.net mvc。我认为我的路线数据中有错误的内容,但我不确定它是错误的还是正常的。我会解释一下情况。 我使用生成的键在 Cache 中缓存我的操作结果(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();
}
,例如:对于 HomePage ,生成的缓存键是 P-Controller=Home.Action=Index 并且
我的站点管理员中也有子操作,例如 LoginBox(位于 MembershipController/ 中)登录框) 它的缓存键是C-Controller=Membership.Action=LoginBox。
到目前为止一切都还好。
我的网站上还有子类别,例如 域/类别1 域/类别1/子类别1 域/类别1/子类别2 域/类别2
当我浏览域/类别1 中的子类别时 我生成的密钥失败,因为我的路由数据错误
filterContext.RouteData.Values: 控制者 = 会员资格 操作 = 登录框 ctg1 = 类别1 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 方法,它正在从数据库检查 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;
}
}
I am using asp.net mvc for my website project. I think i have wrong things in my routedata but i am not sure it is wrong or ok. i will explain the situation.
I am caching my action results (html outputs) in Cache with a generated key
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: For HomePage , generated cache key is P-Controller=Home.Action=Index and
I have also childactions in my sitemaster like LoginBox(It is in MembershipController/LoginBox)
Its cache key is C-Controller=Membership.Action=LoginBox.
Everything is okey till now.
I have also subcategories in my website like
domain/category1
domain/category1/subcategory1
domain/category1/subcategory2
domain/category2
When i am browsing a sub category from domain/category1
My generated keys are failed because my routedatas are wrong
filterContext.RouteData.Values:
Controller = Membership
Action = LoginBox
ctg1 = category1
ctg2 = ""
ctg3 = ""
Why these are mixed. It is using the "Category" routemapping but I think it must use "Default" routemapping.
My global.asax like below
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 = @"[^\.]*" }
);
Also my CategoryRouteConstraint Method it is checking from db that ctg0 value is a category name
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望这可以帮助您,它会告诉您 url 匹配哪些路由,我对这个问题有点困惑。
http://haacked.com/archive/2008/03/13 /url-routing-debugger.aspx
Hopefully this may help you, it will show you which routes a url matches, I was a little confused by the question.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx