The System.Uri class truncates the trailing '.' characters

If I create an instance of the Uri class from a string with the final complete stops - ".", They are truncated from the resulting Uri object.

For example, in C #:

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

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

The escape "." with "% 2E" did not help.

How to make Uri class save time period characters?

+8
c # uri escaping
source share
1 answer

You can use reflection before the call 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); 

This was sent by Connect , and the workaround above was posted there.

+8
source share

All Articles