HttpContext.Request.Path seems to decode percent-encoded URLs sometimes, but not all the time and I'm struggling to understand its behaviour. The documentation says nothing about encoding. I expect it to never decode the path, but testing on ASP.NET Core 3.1 I get results like this:
| Request URL | context.Request.Path | Percent decoded? |
|---|---|---|
http://localhost:5000/test%24 |
/test$ |
Yes |
http://localhost:5000/test%24aa |
/test$aa |
Yes |
http://localhost:5000/test%25a |
/test%25a |
No |
http://localhost:5000/test%25aa |
/test%aa |
Yes |
http://localhost:5000/test%20aa |
/test%20aa |
No |
Is this a bug or is there some reason behind it?
Generated by creating an empty ASP.NET Core project in VS 2019 and changing the Startup class to
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
Console.WriteLine("Request path = " + context.Request.Path);
await context.Response.WriteAsync("Request path = " + context.Request.Path);
});
}
}
Program class unchanged from the template:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
Edit: answer from an ASP.NET contributor: https://github.com/dotnet/aspnetcore/issues/30655