Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Uri class truncates trailing '.' characters

If I create a Uri class instance from string that has trailing full 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.../".

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

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

like image 866
IT Hit WebDAV Avatar asked Jun 05 '11 00:06

IT Hit WebDAV


People also ask

What is System URI?

A URI is a compact representation of a resource available to your application on the intranet or internet. The Uri class defines the properties and methods for handling URIs, including parsing, comparing, and combining. The Uri class properties are read-only; to create a modifiable object, use the UriBuilder class.

What is URI string in C#?

Uri(String, UriKind) Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate. public: Uri(System::String ^ uriString, UriKind uriKind); C# Copy.


1 Answers

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

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

like image 167
keyboardP Avatar answered Sep 21 '22 04:09

keyboardP