Normalize URIs to work correctly with MakeRelativeUri

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())

Here's the conclusion:

../../xxx/xxx.asp

Which looks right, almost all web servers process two of the following requests:

http://www.example.com/test//test.asp
http://www.example.com/test/test.asp

What is the best way to fix this behavior - is there any API for this or manually create a new URI and delete all // in the path?

+5
source share
2 answers

It seems like the best solution is to replace all multisets with one slash in LocalPath. IIS does this even when passing the URL to ASP.net.

The 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));
+4
source

Firstly, your code is not C #, but VB, so the tags are wrong.

, "" URL-, // ?

var x2 = new Uri(x, rel);

, ../ URL?

MakeRelativeUri RFC, . URL- , .

x = new Uri(Regex.Replace(x.OriginalString, "[^:]//", "/"));
0

All Articles