Search code examples
c#uri

Why does the URI constructor remove part of path from the baseUri argument?


public class Program
{
    public static void Main()
    {
        Uri baseUri = new Uri("http://localhost:7777/BasePath/");
        Uri uri = new Uri(baseUri, "/controller");
        Console.WriteLine(uri);
    }
}

Is it the intend behavior to wipe /BasePath out from uri and the final result be http://localhost:7777/controller?


Solution

  • I had to dig into the documentation for the constructor you're calling.

    public Uri (Uri baseUri, string relativeUri);

    Additionally, if the relativeUri begins with a slash, then it will replace any relative part of the baseUri.

    It's the intended behavior. If you specify a relative path that begins with a slash, it assumes that the relative path is the entire relative path, so it discards any relative path already included in baseUri.