MVC RouteDatas 很混乱

发布于 2024-10-06 03:13:55 字数 2493 浏览 1 评论 0原文

我正在为我的网站项目使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺ぐ爱和你が 2024-10-13 03:13:55

希望这可以帮助您,它会告诉您 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文