Search code examples
asp.net-core

Running ASP.NET Core exe from MacOS Finder produces error: "The WebRootPath was not found"


I have published an ASP.NET Core app for MacOS. If I run it from the terminal (./BlazorApp1) it works fine. But if I try to run the exe from Finder I always get this error message:

The WebRootPath was not found: /Users/scott/wwwroot. Static files may be unavailable

Is it possible to run the exe produced from Finder?


Publish settings:

Configuration: Release

Target Framework: net8.0

Deployment Mode: Self-Contained

Target Runtime: osx-arm64

Options:

Enable ReadyToRun compilation

Produce single file


Solution

  • You can fix the issue by using below settings in MacOS.

    // For OSX
    if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
    {
        app.UseStaticFiles(new StaticFileOptions
        { 
            FileProvider = new PhysicalFileProvider(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "wwwroot")), RequestPath = "" });
        }
    // For Windows
    else {
        app.UseStaticFiles();
    }