usr
usr

Reputation: 171206

How to Compare URLs Case-Insensitively?

I'm trying to match the request URL sent to my ASP.NET application case-insensitively because bots or users might mistakenly request a non-canonical form and I need to redirect them.

Which one of the comparison modes is semantically the best choice?

StringComparison.OrdinalIgnoreCase
StringComparison.InvariantCultureIgnoreCase
StringComparison.CurrentCultureIgnoreCase

Ordinal might be to restrictive because it does code-point matching. Might not account for all linguistic variations. CurrentCulture does not seem right for URLs because URLs are supposed to not be culture-specific. InvariantCulture also does not fully make sense because it represents an English-like culture that does not exist on the planet. I'd rather not use it for user-facing strings, not even for the URL in the browser.

Which mode is the right one?

Upvotes: 0

Views: 78

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134035

As you've pointed out, there's no perfect solution. I've always used InvariantCulture and have been happy with the results. But then, my URLs are typically English or English-like, so it's pretty hard to encounter a problem.

Another possibility would be to write a handler or module that intercepts the request and gets the user's culture from the HTTP headers. It could then use that culture to do the transformation. Maybe.

This is one of those things you could spend forever on. I just selected InvariantCulture and figured I'd revisit the decision if I ever have a problem. So far, nobody's noticed. At least, nobody's complained about it.

Upvotes: 1

Related Questions