Lower case route values

2 views
Skip to first unread message

sheepsteak

unread,
Dec 16, 2010, 2:23:11 PM12/16/10
to magellan-friends
Hi

I've noticed that when using lower case routes values like this:

routeCatalog.MapRoute("home", new { controller = "home", action =
"dashboard" });

if I then use the helpful Navigator extension:

navigator.Navigate<HomeController>(x => x.Dashboard());

then I get an UnroutableRequestException that points out that it can't
match the "Home" and "Dashboard" request values to the route value
"home and "dashboard". It's obviously just a string casing issue and I
narrowed down to where it's getting rejected to lines 117-131 of the
ParsedRoute class. It's just using normal equality comparing for
objects so the different cased strings are not matching. Is it
possible to change this so the values are checked if they are strings
and then a case invariant check is done? I've had a look at it myself
and was going to implement it but wondered if it would have any knock-
on effects. It's the code below I think:

foreach (var leftOverItem in leftOver)
{
var key = leftOverItem.Key;
if (Defaults.ContainsKey(key))
{
var leftOverValue = leftOverItem.Value;
var defaultValue = Defaults[leftOverItem.Key];

if ((leftOverValue == null || defaultValue ==
null) && (leftOverValue != defaultValue)
|| (leftOverValue != null && !
leftOverValue.Equals(defaultValue)))
{
return PathMatch.Failure(route,
string.Format("The route was a close match, but the value of the '{0}'
parameter was expected to be '{1}', but '{2}' was provided instead.",
key, defaultValue, leftOverValue));
}
}
}

Cheers
Chris

sheepsteak

unread,
Dec 16, 2010, 3:01:23 PM12/16/10
to magellan-friends
I've had a go myself and come up with:


var leftOverValue = leftOverItem.Value;
var defaultValue = Defaults[leftOverItem.Key];

var leftOverValueString = leftOverValue as string;
var defaultValueString = defaultValue as string;

if (leftOverValueString != null &&
defaultValueString != null)
{
if (string.Compare(leftOverValueString,
defaultValueString, StringComparison.InvariantCultureIgnoreCase) == 0)
{
continue;
}
}

if ((leftOverValue == null || defaultValue ==
null) && (leftOverValue != defaultValue)
|| (leftOverValue != null && !
leftOverValue.Equals(defaultValue)))
{
return PathMatch.Failure(route,
string.Format("The route was a close match, but the value of the '{0}'
parameter was expected to be '{1}', but '{2}' was provided instead.",
key, defaultValue, leftOverValue));
}

It seems to do the trick. I'll update if anything goes wrong...
Reply all
Reply to author
Forward
0 new messages