Asp.net mvc MVC路由协议很混乱
我正在使用asp.NETMVC进行我的网站项目。我认为我的路线数据中有错误的东西,但我不确定它是错的还是好的。我会解释情况。 我使用生成的键在缓存中缓存我的操作结果(html输出)Asp.net mvc MVC路由协议很混乱,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我正在使用asp.NETMVC进行我的网站项目。我认为我的路线数据中有错误的东西,但我不确定它是错的还是好的。我会解释情况。 我使用生成的键在缓存中缓存我的操作结果(html输出) public static string GetKeyFromActionExecutingContext(ControllerContext filterContext) { StringBuilder keyBuilder = new StringBuilder();
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-Controller=Home.Action=Index和
我的sitemaster中也有类似LoginBox的子操作(它位于MembershipController/LoginBox中)
它的缓存键是C-Controller=Membership.Action=LoginBox
直到现在一切都很好
我在我的网站上也有类似的子类别
域/类别1
域/类别1/子类别1
域/类别1/子类别2
域/类别2
当我从域/类别1浏览子类别时
我生成的密钥失败,因为我的routedatas错误
filterContext.RoutedData.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方法正在从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;
}
}
希望这可以帮助你,它会告诉你哪个路由的url匹配,我有点困惑的问题