Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed resource files at build in Visual Studio 2017?

I've a nuget package that has his own mvc control pannel, with controllers, views and routes.

This nuget package is imported in other .net core web applications.

At Visual Studio 2015, with .net core, I used the following code to compile the views as resources, allowing them to be found by razor engine and then rendered correctly.

At project.json (nuget):

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

At 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 exists anymore, and I'm unable to find find a new way to embed my views in the nuget package.

How can I embed my views?

like image 829
Carlos Balsalobre Avatar asked Mar 08 '17 18:03

Carlos Balsalobre


People also ask

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

How do I add resources to Visual Studio 2017?

In Visual Studio, open a SharePoint solution. In Solution Explorer, choose a SharePoint project node, and then, on the menu bar, choose Project > Add New Item. In the Add New Item dialog box, choose the Global Resources File template, and then choose the Add button.

How do you set a building action to an embedded resource?

Click in the solution explorer the file that will be modified. Then hit F4 or click with the right button in the file and then select the "Properties" option. In the Properties window, change Build Action to Embedded Resource.


1 Answers

In Solution Explorer right click on desired file, click Properties and in opened window set Build action to Embedded resource - all like in old days :)

This will create following lines to your *.csproj file:

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

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

like image 136
Dmitry Avatar answered Sep 27 '22 18:09

Dmitry