0

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?

KLIM8D
  • 582
  • 1
  • 8
  • 25

3 Answers3

1

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.

Tallmaris
  • 7,605
  • 3
  • 28
  • 58
  • Thanks a lot. Solved the problem! The page is a string aswell, but since the categories allways will be the same I used them in the regex instead of the number check. "^((Category1)||(Category2)||(Category3))" – KLIM8D Oct 21 '12 at 12:56
0

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.

Stan
  • 25,744
  • 53
  • 164
  • 242
  • Well, I've hoped it was possible. The navigation links looks like this: /activities/2012 (category) --- /activities/calendar (page) Is it because it passing through all the routes again, even though I specified which one it should use? – KLIM8D Oct 20 '12 at 15:00