ASP.NET Core 获取主机名时的 "Decoded string is not a valid IDN name" 错误
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET Core 获取主机名时的 "Decoded string is not a valid IDN name" 错误
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在 ASP.NET Core 中通過 Request.Host.Host 獲取主機名(hostname)時,如果主機名中包含非 ASCII 字符(比如 puny code),就會引發下面的異常:
System.ArgumentException : Decoded string is not a valid IDN name. (Parameter 'ascii')
Stack Trace:
at System.Globalization.IdnMapping.ThrowForZeroLength(Boolean unicode)
at System.Globalization.IdnMapping.GetUnicodeCore(String asciiString, Char* ascii, Int32 count)
at System.Globalization.IdnMapping.GetUnicode(String ascii, Int32 index, Int32 count)
at System.Globalization.IdnMapping.GetUnicode(String ascii, Int32 index)
at System.Globalization.IdnMapping.GetUnicode(String ascii)
at Microsoft.AspNetCore.Http.HostString.FromUriComponent(String uriComponent)
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Host()
改用 Request.Headers[HeaderNames.Host].ToString() 可以避開這個問題,但需要注意一個地方,用這個方法獲取時,如果 url 中包含端口號(比如 https://localhost:5000),得到的主機名中也會包含端口號,比如 localhost:5000 。
總結
以上是生活随笔為你收集整理的ASP.NET Core 获取主机名时的 "Decoded string is not a valid IDN name" 错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [2020-2021 ICPC Sout
- 下一篇: 平台架构用户系统