I found a weirdness with URI class in my .net framework 4.8 project.
The strange thing is that the same code works differently in my simple console application and in the local copy of the production application. Both are running on the same computer, and on the same framework version.
Here is the code:
var test = new Uri("https://myhost.net/Publications/te%21st%C3%87.pdf");
Console.WriteLine(test.AbsolutePath);
So the filename is te!stÇ.pdf
. And Ç
is Turkish letter.
Everything works great in my local app.
But in my local copy of production app (which is running in IIS) it returns a different output:
Application | Output |
---|---|
Local app | /Publications/te%21st%C3%87.pdf |
Local copy of prod | /Publications/te!st%C3%87.pdf |
So it decodes %21
to !
for some reason. But if i replace Turkish Ç
with English C
then %21
is not decoded. Any ideas what is going on?
It looks like I found the cause of the problem and found a way to reproduce/solve it.
This seems to only happen in the WEB apps (API in my case) and not console apps. And targetFramework
in httpRuntime
should be less than 4.7.2
.
Here is my original web.config
:
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
So targetFramework="4.5.2"
in httpRuntime
was causing this weird behavior.
After i changed 4.5.2
to 4.8
, the code new Uri("https://myhost.net/Publications/te%21st%C3%87.pdf").AbsolutePath
returns /Publications/te%21st%C3%87.pdf
instead of /Publications/te!st%C3%87.pdf
The issue is not reproducible when version is 4.7.2
or higher. Looks like they fixed that in 4.7.2 despite the fact that they did not explicitly say this in the release notes: https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/runtime/4.7.x#net-framework-472