2

It appears that in .NET 3.5 and below, and in ASP.NET, the following url is unescaped. Is there a way to avoid this behaviour?

    var strUrl = "https://url.com/file%2F.pdf";
    var uri = new Uri(strUrl);
    var strUri = uri.ToString();

    if (strUrl != strUri)
    {
        throw new Exception("bug!"); // Happens in below .NET 4
    }
Fiach Reid
  • 6,149
  • 2
  • 30
  • 34
  • You could use [Uri.OriginalString](https://learn.microsoft.com/en-us/dotnet/api/system.uri.originalstring) for comparison, instead of `.ToString()`. But I'm not sure this is what you actually want. – Jimi Nov 26 '18 at 18:42

3 Answers3

0

Such a change in behaviour (however small) is still a breaking change, meaning it will never be fixed in future 3.5 releases (yes, 3.5 SP1 is still under support).

To overcome the issue, you'll either have to implement a custom workaround, or find a library or package that does it for you, or upgrade to a newer .NET version.

Peter B
  • 22,460
  • 5
  • 32
  • 69
0

you can use html decode before passing it to URI

Usman Asif
  • 320
  • 2
  • 12
0

I found the solution for .NET 3.5 using reflection using the following code:

if (strUrl != uri.ToString())
{
 var fm_Info = typeof(Uri).GetField("m_Info", BindingFlags.NonPublic | BindingFlags.Instance);
 var m_Info = fm_Info.GetValue(uri);
 var tUriInfo = typeof(Uri).GetNestedType("UriInfo", BindingFlags.NonPublic | 
 BindingFlags.Instance);
 var fString = tUriInfo.GetField("String");
 fString.SetValue(m_Info, strUrl);
}

Ideally, upgrading the version of .NET would be the best solution, but for those who cannot, this may help.

Fiach Reid
  • 6,149
  • 2
  • 30
  • 34