KLIM8D
KLIM8D

Reputation: 583

Redirect to route gives page loop

I'm trying to redirect to another route, if a variable is null. How ever it gives redirect loop error.

The routes:

routes.MapPageRoute("activities", "activities/{category}", "~/Pages/showAllActivities.aspx");
routes.MapPageRoute("activitiesSubPage", "activities/{page}", "~/default.aspx");

Code on showAllActivities.aspx:

if (category != null)
{
    ..
}
else
    Response.RedirectToRoute("activitiesSubPage", new { page = "1"});

Both routes URL need to start with "activities".

How can I accomplish this?

Upvotes: 0

Views: 1229

Answers (3)

Tallmaris
Tallmaris

Reputation: 7590

If the category and page parameters in the route are both numbers, there is no way ASP.NET can discern, so, for example, /activities/2 will be matched by the first route and processed...

If they are different, for example category is a string and page is a number, you have this overload for MapPageRoute where you can provide default values and contraint for the rule (for example for the /activities/{page} route to accept only numbers:

routes.MapPageRoute("activitiesSubPage", "activities/{page}", "~/default.aspx",
    new RouteValueDictionary() { { "page", 0 } },
    new RouteValueDictionary() { { "page", "[0-9]+" } });
);
routes.MapPageRoute("activities", "activities/{category}", "~/Pages/showAllActivities.aspx");

Bear in mind that this configuration will send the /activites route to /activities/0, being 0 the default. I have put the {page} route above so it gets evaluated first and anything that goes past gets intercepted by the next rule.

Upvotes: 1

Stan
Stan

Reputation: 26511

Problem is that it does not know if he passes category or page.

What you can do is map it to something like /activities/category/{id} (category id/name) and /activities/page/{id} (page id). It does not make sense to put /activities/{id} because it's not activity id, but page number.

Upvotes: 0

Related Questions