c# - System.Uri 类截断尾随 '.' 个字符

标签 c# .net asp.net escaping uri

如果我从具有尾随句号“.”的字符串创建 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/

相关文章:

c# - 如何在 MVC 模型中表示一个月的复选框

c# - 解析 JSON.net 中的枚举

浏览器上的 C#/WPF 图像叠加

c# - RichTextBox - InvalidOperationException : The calling thread must be STA

.net - 为什么asp.net 成员(member)有一个users 表和一个membership 表?

c# - 指定 xUnit Theory 测试的返回值

c# - 保存/检索图像。 ASP.NET MVC

c# - CLR调试器和 'var'

c# - 转换日期时间格式

asp.net - 如何在asp.net中显示页面生成时间?