Search code examples
asp.net-coreasp.net-core-mvc.net-assemblyclass-librarymvc-editor-templates

EditorTemplate templates in a separate assembly


Is it possible to configure an ASP.NET Core web application to use EditorTemplate razor pages that are compiled into a class library/separate assembly?

As an example, the following question details how a ViewComponent can be compiled into a class library. Unfortunately I can't find any information about doing the same with EditorTemplate pages.

ASP.NET MVC 6: view components in a separate assembly


Solution

  • Solved this with help from pranavkm on GitHub. The steps I required were as follows:

    In the web application: in ConfigureServices() add the class module assembly as a file provider (get the assembly through the type of any class or interface within the assembly):

    var library = typeof(Library.SomeClass).GetTypeInfo().Assembly;
    services.AddMvc()
        .AddRazorOptions(options =>
        {
            options.FileProviders.Add(new EmbeddedFileProvider(library));
        });
    

    In the class module: ensure the directory structure that contains your editor templates is the same as it would be in your web application Views\Shared\EditorTemplates.

    In the class module: embed the razor views as a resource by adding the following to your project.json:

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