Yucel
Yucel

Reputation: 2673

MVC RouteDatas are confused

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;
    }


}

Upvotes: 0

Views: 116

Answers (1)

BlackICE
BlackICE

Reputation: 8926

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

Upvotes: 0

Related Questions