dotnet에서 uris를 구성하는 것은 생각보다 어렵습니다.
HttpClient를 사용한 다음 모든 요청에 대해 상대 경로를 추가하는 것입니다. 기본 주소에 경로의 일부가 포함되어 있으면 슬래시가 추가되는 위치에 매우 주의해야 합니다. 그렇지 않으면 결과 URI가 예상한 것과 다를 수 있습니다.우리는 최근 한 서비스에서 다른 서비스로 이동하는 모든 요청에 대해 갑자기 404를 받기 시작한 직장에서 이것에 대해 조금 알게 되었습니다.
결과적으로 우리는 구성을 변경했고 기본 주소에서 후행 슬래시가 누락된 것이 그 이유였습니다.
이 작은 샘플은 무슨 일이 일어났는지 보여줍니다
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var baseAddresses = new [] { "http://host.io/foo", "http://host.io/foo/" };
var relativeAddresses = new [] { "bar", "/bar" };
foreach (var baseAddress in baseAddresses)
{
foreach (var relativeAddress in relativeAddresses)
{
var uri = new Uri(new Uri(baseAddress), relativeAddress).AbsoluteUri;
Console.WriteLine($"{baseAddress} + {relativeAddress} = {uri}");
}
}
}
}
그리고 출력은
http://host.io/foo + bar = http://host.io/bar
http://host.io/foo + /bar = http://host.io/bar
http://host.io/foo/ + bar = http://host.io/foo/bar
http://host.io/foo/ + /bar = http://host.io/bar
보시다시피 가능한 조합 4개 중 3개에서 기본 주소의
foo 부분이 최종 URI에서 자동으로 제거됩니다. 😱
Reference
이 문제에 관하여(dotnet에서 uris를 구성하는 것은 생각보다 어렵습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/jonassamuelsson/constructing-uris-in-dotnet-is-harder-than-it-should-35ep텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)