Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Single File ASP.NET Core 5 web app does not load static resources

I'm trying to create a single file asp.net core 5 web app. Goal is to have a single .exe file, run the Kestrel server by executing this exe file and load the page in the browser.

I created an ASP.NET Core 5 template app in VS 2019. Then using cli I run this command:

dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true

This generates an exe file, which when I copy elsewhere, runs without a problem. But when I browse the page, none of the static files are loaded:

enter image description here

What would be the proper way of generating a single file asp.net core app, so it loads static content ?

EDIT

As requested, putting here the screenshot of the output after the publish

enter image description here

EDIT 2

To get a reproducible project:

Visual Studio 2019 -> New Solution -> ASP.NET Core Web App with the configuration below

enter image description here

EDIT 3

Thanks to the answer by @JHBonarius, I changed the Program.cs to set ContentRoot to a temp folder where wwwroot content is getting extracted.

public class Program
    {
        public static void Main(string[] args)
        {
            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);

            var directory = 
                Directory
                    .GetDirectories(path)
                    .Select(path => new DirectoryInfo(path))
                    .OrderByDescending(di => di.LastWriteTime)
                    .First();

            CreateHostBuilder(args)
                .UseContentRoot(directory.FullName)
                .Build()
                .Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)            
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

While I appreciate that this seems like a hack (I couldn't find any official documentation for this path), I wanted to have some working code.

With these changes page now loads static resources, not all of them though.

This is the content of wwwroot folder in the solution explorer

enter image description here

And this is the content of extracted wwwroot folder on the temp path

enter image description here enter image description here

As can be seen js/css folders are missing altogether as well as jquery-validation & jquery-validation-unobtrusive folders.

Any clue what's going on ?

I created a github repo with latest changes.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I might reproduced you issue.

I published the project, and open a command line at an irrelevant folder, open the exe with full path. Then the static resource are not loaded.

You can try to double click the exe, check if it works.

To fixed the issue:

Add Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); in the Program.cs

over 4 years ago · Santiago Trujillo Report

0

I found the solution here

Add the following to the csproj

<ItemGroup>
    <Content Update="wwwroot\**" ExcludeFromSingleFile="false">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

Because what happens, is that the .exe contains an archive, which is extracted to a temporary directory.

However, this only works for .NET Core (3.1). They changed the behavior in .NET 5, where most dependencies are now directly loaded from the .exe or memory. So the content is now considered an external dependency, and the content path is set to the excuting directory, instead of the temporary directory.

The solution I mention will still include the wwwroot directory in the .exe and extract it to the temporary path, but since the content path is not pointing there, the content is not found.

You can change the content path using .UseWebRoot("[path]"). However, I haven't found a way to get the path of the temp directory.

edit: there's a whole other option: put the wwwroot files in a zip file, add the zip file as Embedded Resource, and serve it using a static file provider.

over 4 years ago · Santiago Trujillo Report

0

According to requests all static files were requested without wwwroot path so either configure your html & js and add wwwroot in path or explicitly map wwwroot content like this:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot")),
    RequestPath = new PathString("/wwwroot")  // OR use "/" to map to base URI
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!