-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Asp.Net Core MVC] Is posssible to embed the wwwroot directory inside the dist binary? #55657
-
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
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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 😀
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1 -
❤️ 2
-
Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions
-
It's possible to use with WPF or WinForms?
Beta Was this translation helpful? Give feedback.