.NET Uri:使用构造函数组合路径的意外 Uri

.NET Uri: unexpected Uri using constructor to combine paths

我正在使用构造函数:

public Uri(Uri baseUri, string relativeUri)

将相对路径附加到初始 Uri。 通常,一切正常,并附加了所需的路径,但在某些情况下,最终路径被替换。

例如,使用此代码:

新 Uri(新 Uri("http://localhost:3000/app/api/publicapi/NAS_0x5d65d971895edc438f465c17db6992698a52318d"), "Blocks")

我期望这样的结果:

http://localhost:3000/app/api/publicapi/NAS_0x5d65d971895edc438f465c17db6992698a52318d/Blocks

但我得到:

http://192.168.26.50:3000/app/api/publicapi/Blocks

这里怎么了?

缺少最后一个斜杠,因此它将 NAS_0x5d65d971895edc438f465c17db6992698a52318d 视为资源而非路径:

var existingUri =
    new Uri("http://localhost:3000/app/api/publicapi/NAS_0x5d65d971895edc438f465c17db6992698a52318d/");

new Uri(existingUri, "Blocks");
// returns: http://localhost:3000/app/api/publicapi/NAS_0x5d65d971895edc438f465c17db6992698a52318d/Blocks

来自docs

If the baseUri has relative parts (like /api), then the relative part must be terminated with a slash, (like /api/), if the relative part of baseUri is to be preserved in the constructed Uri.