Thursday, April 1, 2010

Remember to compare Url's with System.Uri

@YaronNaveh

When you need to compare two Url's never trust on simple string comparison. Always use a comparison that takes semantics into consideration. In C#, System.Uri can be used. Check the below:


string url1 = "http://www.site.com/dir/index.html";
string url2 = "http://www.site.com/dir/dir1/../index.html";

url1 == url2; //false
new Uri(url1).Equals(url2); //true


And another one:


string url1 = "http://www.site.com/index.html?q=1 2";
string url2 = "http://www.site.com/index.html?q=1%202";

url1 == url2; //false
new Uri(url1).Equals(url2); //true

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

0 comments: