如果我从具有尾随句号“.”的字符串创建 Uri 类实例,它们将从生成的 Uri 对象中截断。
例如在 C# 中:
Uri test = new Uri("http://server/folder.../");
test.PathAndQuery;
返回“/folder/”而不是“/folder.../”。
转义“。”使用“%2E”没有帮助。
如何使 Uri 类保留尾随句点字符?
最佳答案
您可以在调用代码之前使用反射。
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
Uri test = new Uri("http://server/folder.../");
Console.WriteLine(test.PathAndQuery);
已提交至Connect上面的解决方法已发布在那里。
关于c# - System.Uri 类截断尾随 '.' 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240203/