Inconsistent PathBase when running as IIS sub-application
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
# Describe the bug
Response.OnStarting does not observe the same PathBase as the rest of the pipeline on empty-body responses.
### To Reproduce
launchSettings.json
``` JSON
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:37035/NFE",
"sslPort": 44359
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
```
Program.cs
``` C#
public class Program
{
public static void Main(string[] args)
{
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.Build()
.Run();
}
}
```
Startup.cs
``` C#
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
app.Use((context, next) =>
{
context.Response.Headers.Append("X-PATHBASE1", ">>>" + context.Request.PathBase.Value);
context.Response.OnStarting(() =>
{
context.Response.Headers.Append("X-PATHBASE2", ">>>" + context.Request.PathBase.Value);
return Task.CompletedTask;
});
return next();
});
app.Run(context => Task.CompletedTask);
}
}
```
Project.csproj (2.1)
``` XML
net48
```
Project.csproj (3.0)
``` XML
netcoreapp3.0
```
Run the app, run any request, then observe the response headers. The PathBase observed in the OnStarting callback is empty.
#### expected headers
X-PATHBASE1: >>>/NFE
X-PATHBASE2: >>>/NFE
#### actual headers
X-PATHBASE1: >>>/NFE
X-PATHBASE2: >>>
#### actual headers when using response body e.g. `app.Run(context => context.Response.WriteAsync(""));`
X-PATHBASE1: >>>/NFE
X-PATHBASE2: >>>/NFE
### Additional remarks
The root of the problem seems to be how IIS integration is imlemented. IISSetupFilter/IISServerSetupFilter injects the UsePathBase middleware early in the pipeline which sets the proper PathBase from IIS metadata. However if there is no response body then OnStarting executes after all middlewares have completed and at this point the UsePathBaseMiddleware have reverted the PathBase to its original empty value.
Possibly related: #5898
### Further technical details
- ASP.NET Core version
both 2.1 and 3.0 seems to be affected
- Include the output of `dotnet --info`
.NET Core SDK (reflecting any global.json):
Version: 3.0.100
Commit: 04339c3a26
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.100\
Host (useful for support):
Version: 3.0.0
Commit: 7d57652f33
.NET Core SDKs installed:
2.1.802 [C:\Program Files\dotnet\sdk]
2.2.402 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
- The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
Visual Studio Community 2019 (16.3.8)
Contributor guide
Assessment
This issue has not been assessed yet.