的System.Uri类截断尾随'。“字符 [英] System.Uri class truncates trailing '.' characters

查看:255
本文介绍了的System.Uri类截断尾随'。“字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从字符串有尾随句号创建Uri类的实例 - '。',它们从产生的Uri对象截断。

If I create a Uri class instance from string that has trailing full stops - '.', they are truncated from the resulting Uri object.

例如在C#:<​​/ P>

For example in C#:

Uri test = new Uri("http://server/folder.../");
test.PathAndQuery;

返回/文件夹/而不是/文件夹... /。

returns "/folder/" instead of "/folder.../".

转义。用%2E没有帮助。

Escaping "." with "%2E" did not help.

如何让我的Uri类保持尾随期字符?

How do I make the Uri class to keep trailing period characters?

推荐答案

您可以在你调用code使用反射。

You can use reflection before your calling code.

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);

这已经提交给<一个href="http://connect.microsoft.com/VisualStudio/feedback/details/386695/system-uri-incorrectly-strips-trailing-dots">Connect和解决方法上面贴在那里。

This has been submitted to Connect and the workaround above was posted there.

这篇关于的System.Uri类截断尾随'。“字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆