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:
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:
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?
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.
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
).