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:
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
EDIT 2
To get a reproducible project:
Visual Studio 2019 -> New Solution -> ASP.NET Core Web App with the configuration below
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
And this is the content of extracted wwwroot folder on the temp path
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.







PublishSingleFileoption?ItemGroupto contain static files when deploy.