Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Views in a Razor Class Library aren't found

I have a simple RCL with following structure:

/Pages /Shared/ _Footer.cshtml

The content is very simple:

<h3>  _Footer.cshtml partial view.</h3>

I also have a ASP.NET Core Web App project with same folder structure:

/Pages _ViewImports.cshtml -ViewStart.cshtml /Shared _Layout.cshtml

Somewhere in the layout file I make a reference to the partial view:

<partial name="_Footer" />

Here is the problem:

  • If I add a Project Reference of RCL to the Web App, the partial view is found and pages render fine.
  • If I make a Nuget package from the RCL, add it to a local Nuget source and add a reference to the package, the partial view won't be found.

The partial view '_Footer' was not found. The following locations were searched: /Pages/_Footer.cshtml /Pages/Shared/_Footer.cshtml /Views/Shared/_Footer.cshtml

What could be the difference between adding a project reference and Nuget reference? I've verified the Nuget package does contain both the Class Lib's default and views assemblies.

like image 990
Amir Keibi Avatar asked Sep 21 '18 21:09

Amir Keibi


4 Answers

According to the docs (https://docs.microsoft.com/en-us/aspnet/core/razor-pages/sdk), I think you can use IncludeRazorContentInPack to include your .cshtml files in a NuGet package (it defaults to false).

like image 104
crgolden Avatar answered Sep 19 '22 09:09

crgolden


In my case I wasn't even able to make it work in the scenario the op said it does work:

If I add a Project Reference of RCL to the Web App, the partial view is found and pages render fine.

I was just getting the standard error message that the partial view was not found (even if the searched paths were correct).

After some head banging I solved this problem by adding the following xml block to both .csproj files:

  1. RCL: e.g. RazorUIClassLib.csproj
  2. Web App: e.g. WebApp1.csproj
<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
like image 36
infografnet Avatar answered Sep 19 '22 09:09

infografnet


I had the same problem, the solved writing the full path from the library Area, for example:

In the library i have:

Areas/Footer/Pages/_footer.cshtml

with my UI Proyect i called it like: <partial name="~/Areas/Footer/Pages/_footer.cshtml" model="My Model instance"/>

I hope it can help you

like image 33
Jose luis Flórez Avatar answered Sep 17 '22 09:09

Jose luis Flórez


I found it works if you use dotnet pack instead of nuget pack. Note for dotnet pack package information is now read from csproj instead of nuget spec file.

like image 45
Michael Avatar answered Sep 20 '22 09:09

Michael