In Global.asax.cs in a project that I'm maintaining there is a method that looks like this
private static void MapRouteWithName(string name, string url, string physicalPath, bool? checkPhysicalUrlAccess = null, RouteValueDictionary defaults = null)
{
Route route;
if ((checkPhysicalUrlAccess != null) && (defaults != null))
{
route = System.Web.Routing.RouteTable.Routes.MapPageRoute(name, url, physicalPath, checkPhysicalUrlAccess.Value, defaults);
}
else
{
route = System.Web.Routing.RouteTable.Routes.MapPageRoute(name, url, physicalPath);
}
route.DataTokens = new RouteValueDictionary();
route.DataTokens.Add("RouteName", name);
}
It's then used in Global.asax.cs to set up routes like this:
MapRouteWithName("Change User", "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}", "~/User/Change.aspx", true,
new RouteValueDictionary { { "id", "0" }, { "organizationid", "0" }, { "username", "" }, { "logonaccount", "" }, { "phone", "" } });
And then in the code behind a page one can find things like this:
Response.RedirectToRoute("Change User", new
{
id = userID,
organizationid = selectedOrganizationID,
username = userName,
logonaccount = logonAccount,
phone = phone
});
Now this works nicely in most cases but in this particular example I don't know if the variables userName, logonAccount and phone actually contains something or is empty. When for instance userName is empty ("") and logonAccount has a value ("abcde") an exception is thrown:
System.InvalidOperationException: No matching route found for RedirectToRoute.
It seems like the exception is thrown when there is a variable with an empty string in between variables that has values. (Did this make sense?) What can I do about this?