Partial Views in a Razor Class Library aren't found - razor

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.

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.

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:
RCL: e.g. RazorUIClassLib.csproj
Web App: e.g. WebApp1.csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

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

According to the docs (https://learn.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).

Related

Razor Class Library in ASP .Net Core 2.1.1

I used to embed some of my Razor views for mailing in a class library which was using ASP .Net Core 2.1.0-preview1-final and it was working fine.
Here is the configuration in the .csproj file:
Since I upgraded the .Net Core version to 2.1.1 which is the final one, I can not use the embedded views anymore.
I know about the new Razor Class Library concept but I need to pass my views to a ViewRenderer service which basically converts the view to string to be sent as an email.
The viewrenderer service was finding the views before but now it doesn't find them and I get the following error:
VIEW does not match any available view
How can I fix this issue?
I was facing the exact same issue, setting CopyRefAssembliesToPublishDirectory to true in the csproj file fixed it. your csproj file should look like this :
...
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
...

compile razor views in .net core

Is there a way I can compile all my razor views (to verify) any time I need? I found this doc which shows how it compiles on publish https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-2.1&tabs=aspnetcore2x.
I am looking for an option within visual studio or even better via CLI that compiles and validates all views. I did find this official razor CLI tool in preview, but no documentation: https://www.nuget.org/packages/Microsoft.AspNetCore.Razor.Tools/1.1.0-preview4-final
Well, you can publish any time you need. The name ‘publish’ does not mean ‘push my site to the coliseum of public opinion’ :-)
All that it does stays local, and dotnet publish will by default create a directory under your project bin folder with, as you say, compiled views and other artefacts.
Per this comment, with .NET core 2.1 you can compile razor views at build time without needing to publish, by adding these two lines to the <PropertyGroup> section of your project file:
<RazorCompileOnBuild>true</RazorCompileOnBuild>
<ResolvedRazorCompileToolset>RazorSdk</ResolvedRazorCompileToolset>
This will cause them to compile to a [project].Views.dll, and you'll no longer need to distribute the cshtml files.

Nancy: Razor View Engine #using IntelliSense not working

I have an IntelliSense problem with Nancy and the Razor View Engine.
I try to use the functionality of an (self-made) external library inside my razor view. Therefore I reference the dll and try to put an #using and the namespace on top of the view to get some IntelliSense-Support.
But it doesn't work. The #using can't find the dll or rather the namespace I have to put behind it.
The VS-Error I get is: "The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)"
It just seems to be an IntelliSense problem because the engine is able to render the view if I use the correct #using on top of the view and ignore the error message or if I have a class inside my project which inherits from IRazorConfiguration and returns the needed namespace.
Notes:
Hosting: Self-Host and/or OWIN
It works if the library is part of the same solution
This happens with with Nancy 0.22.2 as well as 0.23 and the corresponding Razor packages.
Steps to reproduce (or better: things I did):
Create a new Console Application
Install the Nancy Razor View Engine package
Reference an external dll (namespace: External)
Reference a dll which is part of the same solution (namespace: Same)
Create a new Razor View
Try: #using External -> not working
Try: #using Same -> working
I hope someone can help me even if It is not a real problem since razor is able to render the view but it's annoying :(
OK, I finally found a solution. The following answer led me to it:
https://stackoverflow.com/a/19653760/1761291
I tried it and it worked but instead of doing this its ok to only copy the external dll into the bin folder.

Problems while trying to use components from another module in intellij idea AS3

i'am trying to use components from another module, but is not working.
Look what i have:
I have my project, its an app to convert files, and its working everything is ok. Now i want to change the interface... for that i cloned a github repository thats is a project with the components that i want to use, and imported it as a module. (should i import as a module or as a project?)
Everything great till now, but when i try to use the components from the module i cant find the classes or even the module...
Any suggestions?
You should add your imported sources as a new module (let's call it B), then you should add a dependency from your original module A to your module B in order to use its code.
See this page on how to configure module dependencies.

How to set up visual studio to create CSHTML files in (Umbraco) Project

So, i'm developing my razor macroscripts in Visual studio for my Umbraco project.
Everything is working fine, but there are two things really annoying.
If I want to make a new CSHTML file the best solution for this is to duplicate an existing file.
I dont have full razor IntelliSense like e.g. Html.Raw
Is there a way to configure my project to use this features? Didn't find a .cshtml template yet.
You need to have the MVC Framework installed, then when you open the project as a website, you should be able to create and edit cshtml files with syntax highlighting. See my answer to the following post for more details:
Setting up local development environment for Umbraco
If your project is a web site/application then the mvc templates aren't available (they only show up in MVC projects). You can just create a text file and name it with the .cshtml extension though (you could set up your own template for this in VS if you wanted to).
To get intellisense in your Razor files, see Doug Robar's blog post on the subject
As an alternative if you go into the Umbraco admin, go to the 'Developer' section and right click on 'Scripting Files' you can create razor scripts directly (and this will save the new .cshtml directly into your 'macroScripts' folder - although in VS2010 you will need to right click on the new script and choose 'include in project').
Also this will allow you to base your new razor macroscript on one of the pre-built snippets so you may get a bit of core functionality for free.
From Umbraco 6 on it's very convenient to install Umbraco on your local file system with Visual Studio and NuGet. Given that you have the MVC Framework installed and you use Visual Studio 2012 or above, you get full Razor support in Visual studio.
Umbraco Our has a great blogpost about this where they described the steps below in detail (with screenshots!).
Create an Empty Web Application.
Install Umbraco using Manage Nuget Packages ('Umbraco CMS') or the Package manager console (Install-Package UmbracoCms)
NuGet will then download dependencies and will install all of Umbraco's files in your new solution. During this process it will ask if it is allowed to overwrite your web.config file. (Make a back up of your existing web.config if you install Umbraco in an existing project)
Finally, don't forget to run your project hitting F5. You'll see that whenever you try to add or edit a file in your views folder you have razor support and intellisense