How to render a partial view located in an application part (DLL) into main asp.net core 2.2 project

1.4k Views Asked by At

I have an ASP.NET Core 2.2 project with its controllers and views. Due to project requirements, I needed to create a Razor Class Library to separate some .cshtml files and controllers from the main project. Look below the project and folder structure:

enter image description here

I'm facing issues when I need to load a partial view located in that Razor Class Library into a view located in the main project:

enter image description here

The app throws 500 error:

An unhandled exception occurred while processing the request. InvalidOperationException: The partial view '_ComponenteDivisionPoliticaPartial' was not found. The following locations were searched: /Areas/Administracion/Views/GeneralPersonas/_ComponenteDivisionPoliticaPartial.es-CO.cshtml /Areas/Administracion/Views/GeneralPersonas/_ComponenteDivisionPoliticaPartial.es.cshtml /Areas/Administracion/Views/GeneralPersonas/_ComponenteDivisionPoliticaPartial.cshtml /Areas/Administracion/Views/Shared/_ComponenteDivisionPoliticaPartial.es-CO.cshtml /Areas/Administracion/Views/Shared/_ComponenteDivisionPoliticaPartial.es.cshtml /Areas/Administracion/Views/Shared/_ComponenteDivisionPoliticaPartial.cshtml /Views/Shared/_ComponenteDivisionPoliticaPartial.es-CO.cshtml /Views/Shared/_ComponenteDivisionPoliticaPartial.es.cshtml /Views/Shared/_ComponenteDivisionPoliticaPartial.cshtml /Pages/Shared/_ComponenteDivisionPoliticaPartial.es-CO.cshtml /Pages/Shared/_ComponenteDivisionPoliticaPartial.es.cshtml /Pages/Shared/_ComponenteDivisionPoliticaPartial.cshtml Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.RenderPartialCoreAsync(string partialViewName, object model, ViewDataDictionary viewData, TextWriter writer)

  • How can I do to reference or use the required partial view?
  • Are the static files in wwwroot such as JS files and CSS recognized by the Partial View into Razor Class Library?
2

There are 2 best solutions below

0
On

Please note that only Razor Pages are supported in the Razor class library (RCL) by default, and if you'd like to support views, the template option "Support pages and views" should be checked while you create new RCL project.

enter image description here

This feature is not supported in ASP.NET Core 2.2 (introduced in ASP.NET Core 3.0), so you got above error.

In this doc about "Template changes", you would find more detailed information like below:

The Razor class library (RCL) template defaults to Razor component development by default. A new template option in Visual Studio provides template support for pages and views. When creating an RCL from the template in a command shell, pass the --support-pages-and-views option (dotnet new razorclasslib --support-pages-and-views).

0
On

I've found a solution:

Despite of the fact as Fei Han mentioned that the support for pages and views is supported since asp.net core 3.0, I achieved to use views and controllers in a library (DLL) by doing following in asp.net core 2.2:

I've checked the type of library project:

<Project Sdk="Microsoft.NET.Sdk.Razor">

I've configured the application part and razor view engine options in order to add file providers accordingly, in startup.cs:

        services.ConfigureApplicationPartManager(c => 
            {
                c.ApplicationParts.Add(new AssemblyPart(typeof(ComponenteDivisionPoliticaController).GetTypeInfo().Assembly));
            }
        );

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.FileProviders.Add(new EmbeddedFileProvider(typeof(ComponenteDivisionPoliticaController).GetTypeInfo().Assembly));
        });

That's it, it's running now