How to embed resource files during assembly in Visual Studio 2017?

I have a nuget package that has its own mvc control panel, with controllers, views and routes.

This nuget package is imported into other .net web applications.

In Visual Studio 2015 with the .net core, I used the following code to compile views as resources, allowing them to be found using the razor mechanism and then displayed correctly.

In project.json (nuget):

"buildOptions": { "embed": "**/Views/**/*.cshtml" } 

In Startup.cs (web application):

 public void ConfigureServices(IServiceCollection services) { services.Configure<RazorViewEngineOptions>(options => { options.FileProviders.Add(new CompositeFileProvider( new EmbeddedFileProvider( typeof(HomeController).GetTypeInfo().Assembly, "Some.Namespace")) ); }); return new IuguPortalBuilder(services); } 

In Visual Studio 2017, the project.json file.doesn't exist anymore, and I cannot find a new way to embed my views in the nuget package.

How can I embed my views?

+5
source share
1 answer

In Solution Explorer right-click on the desired file, click Properties and in the window that opens, set the Build action to the Embedded resource - everything is like in the old days :)

This will create the following lines in your *.csproj file:

 <ItemGroup> <EmbeddedResource Include="Views\Home\Index.cshtml" /> </ItemGroup> 

Now MSBuild will add this file as an embedded resource to your assembly.

+10
source

Source: https://habr.com/ru/post/1265198/


All Articles