dotnet / dotnet/aspnetcore

ASPNETCORE_CONTENTROOT / DOTNET_CONTENTROOT / --contentRoot being ignored when running under IIS

Open
#50,693 0 comments 0 reactions 0 assignees View on GitHub
area-networking feature-iis
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Description

When setting either of the two environment variables in web.config with:
```











```

or trying to use `--contentRoot C:\some\other\path` in the arguments attribute on the `aspNetCore` element they don't get picked up.

It looks like `UseIIS()`, that is being run later in the "pipeline"/code in the `WebHost.CreateDefaultBuilder(args)` overwrites the ContentRoot to use a value received from IIS, and completely resets the WebRoot settings.

### Reproduction Steps

1. Set up a IIS Website / Web Application to a folder where you have only `web.config` located.
```











```

2. Create a sample project in VS and add the following code some where after the application is started, and you have access to the `IWebHostEnvironment`
```
Console.WriteLine($"Application Name: {CurrentEnvironment.ApplicationName}");
Console.WriteLine($"Environment Name: {CurrentEnvironment.EnvironmentName}");
Console.WriteLine($"ContentRoot Path: {CurrentEnvironment.ContentRootPath}");
Console.WriteLine($"WebRootPath: {CurrentEnvironment.WebRootPath}");
```

3. Check the log output and see that WebRoot is empty/null, and ContentRoot is the Path where web.config is located.

### Expected behavior

That the provided value from either of these environment variables / command-line argument to be used.

### Actual behavior

They are ignored / overwritten by the "content root"/website-application path from IIS.

### Regression?

_No response_

### Known Workarounds

I had to specifically add `.UseContentRoot("C:\\CommonCodeFolder\\Version1");` to the `WebHost.CreateDefaultBuilder(args)` to make the app pick up the ContentRoot correctly.
This also fixed the issue with WebRoot path not being set at all.

### Configuration

```
> dotnet --list-runtimes
Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
```

OS: Windows Server 2022 Datacenter (x64)

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.