dotnet / dotnet/aspnetcore

DirectoryNotFoundException when setting a custom WebRoot at runtime

Open
#48,620 2 comments 4 reactions 0 assignees View on GitHub
area-hosting bug
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I have a custom build setup for my static frontend assets. The compiled frontend assets live in a directory outside of the solution and are being included in `` in the csproj in a target dynamically, then being copied over to `wwwroot` in the output directory.

For development from Visual Studio I have added a piece of code which changes the WebRoot to point at the folder with the compiled assets, so that I can recompile them without rebuilding the service.

```csharp
[Conditional("DEBUG")]
private static void ConfigureDevelopmentTimeWebRoot(IWebHostBuilder builder)
{
if (Environment == Environments.Development)
builder.UseWebRoot("../../frontend/dist");
}
```

If I put an empty `wwwroot` in the directory of my C# project it works. If I remove the `wwwroot` folder I get an exception while building the `WebHost`. Note that I'm using ASP.NET Core 7 with the old coding style `WebHost.CreateDefaultBuilder(args)` and enabling static files with `app.UseStaticFiles()` in the `builder.Configure((ctx, app) => {...})`.

Seems like `StaticWebAssetsLoader` finds a note somewhere (in some manifest) that it should look at `wwwroot` in the project folder, rather than respecting WebRoot setting.

I looked briefly in the sources and apparently there's a manifest file being generated in the `obj` folder `staticwebassets.development.json` which specifies those paths - is there some build properties I should be setting? I see in there a lot of files related to ASPNET Core Identity which are not actually present in my build output.

### Expected Behavior

No exception being thrown if the `wwwroot` doesn't exist.

### Steps To Reproduce

_No response_

### Exceptions (if any)

```
System.IO.DirectoryNotFoundException: '...\src\backend\ManagementHub.Service\wwwroot\'
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.<>c.b__1_0(String contentRoot)
at Microsoft.AspNetCore.StaticWebAssets.ManifestStaticWebAssetFileProvider..ctor(StaticWebAssetManifest manifest, Func`2 fileProviderFactory)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.UseStaticWebAssetsCore(IWebHostEnvironment environment, Stream manifest)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.UseStaticWebAssets(IWebHostEnvironment environment, IConfiguration configuration)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at ManagementHub.Service.Program.Main(String[] args) in Program.cs:line 53
```

### .NET Version

7.0.302

### Anything else?

_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.