Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Asp.Net Core MVC] Is posssible to embed the wwwroot directory inside the dist binary? #55657

Discussion options

I have an Asp Net Core MVC project with my assets in wwwroot. My intent is to have only the binary and the appsettings file so is easy to update the application in production without to lose some updated assets files.
I used this options (https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli) the create the binary but the wwwroot is outside. There is any way to embed that folder and calling assets embeded files from cshtml? (I have used this tecnics with the GO language).
Thanks

You must be logged in to vote

Yes, you can do this 😀 Here is how:

In your *.csproj:

<PropertyGroup>
 <!-- all the usual stuff -->
 <PublishSingleFile>true</PublishSingleFile>
 <SelfContained>true</SelfContained>
 <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
 <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<ItemGroup>
 <EmbeddedResource Include="wwwroot\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
 <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.4" />
 <!-- all your other dependencies -->
</ItemGroup>

Next, in your Program.cs:

// all the usual stuff
var builder 

Replies: 1 comment 2 replies

Comment options

Yes, you can do this 😀 Here is how:

In your *.csproj:

<PropertyGroup>
 <!-- all the usual stuff -->
 <PublishSingleFile>true</PublishSingleFile>
 <SelfContained>true</SelfContained>
 <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
 <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<ItemGroup>
 <EmbeddedResource Include="wwwroot\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
 <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.4" />
 <!-- all your other dependencies -->
</ItemGroup>

Next, in your Program.cs:

// all the usual stuff
var builder = WebApplication.CreateBuilder(args);
// all the usual stuff
var fileProvider = new ManifestEmbeddedFileProvider(Assembly.GetAssembly(type: typeof(Program))!, "wwwroot");
var app = builder.Build();
app.UseStaticFiles(new StaticFileOptions
{
 FileProvider = fileProvider,
 RequestPath = string.Empty,
});
// other stuff

That's it 👍. Please notice, that you request these files without the trailing wwwroot. You might define another request path using RequestPath = "/webContent", though.

I hope that helps you 😀

You must be logged in to vote
2 replies
Comment options

Thanks so much!

Comment options

It's possible to use with WPF or WinForms?

Answer selected by stormenergy91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /