Dim x AS New URI("http://www.example.com/test//test.asp")
Dim rel AS New URI("http://www.example.com/xxx/xxx.asp")
Console.Writeline(x.MakeRelativeUri(rel).Tostring())
In here output is:
../../xxx/xxx.asp
Which looks correct almost all web servers will process the two of the following as same request:
http://www.example.com/test//test.asp
http://www.example.com/test/test.asp
What's the best way to fix this behaviour is there any API to do this, or shall manually create a new URI and remove all // in the path?
URL normalization (also called URL canonicalization) is the process by which URLs are modified and standardized in a consistent manner. The purpose of URL normalization is to transform a URL into a normalized or canonical URL so it is possible to determine if two syntactically different URLs are equivalent.
Normalizing a path involves modifying the string that identifies a path or file so that it conforms to a valid path on the target operating system. Normalization typically involves: Canonicalizing component and directory separators. Applying the current directory to a relative path.
It seems that replacing all multi-slashes with one slash in LocalPath portion is the best solution. IIS does so even when passing URL to ASP.net.
Following code will do the trick.
x = new Uri(string.Format("{0}://{1}:{2}{3}{4}", x.Scheme, x.Host, x.Port, Regex.Replace(x.LocalPath, @"(?<!\:)/{2,}", "/"), x.Query));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With