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?
If the category and page parameters in the route are both numbers, there is no way ASP.NET can discern, so, for example,
/activities/2will 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:Bear in mind that this configuration will send the
/activitesroute 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.