I have recently come across Razor Class Libraries and am not 100% sure of what can / can't be achieved with them, but am very much liking the concept of being able to create views (and partial views) which can be shared within multiple web projects.
However, whilst I know a bit of C# and am quite familiar with the conversion tools, I am 1000 times more proficient with VB.Net and consequently most of my work is coded that way. .NET Core, as far as I can deduce does not support VB.NET.
I followed the tutorial and downloaded the sample code from here https://learn.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.1&tabs=visual-studio
With this I was able to build a solution containing a RCL and a .NetCore website which uses views and partial views from the RCL.
I was able to build multiple .NETCore Websites and all I had to do was to add a reference to the RCL Project and they would all use the shared partial views in the RCL... everything worked well.
EXCEPT, I don't want a .NETCore website project because I want to stick with VB.NET, so I read up and am told that the RCL I have created using .netstandard2.0 should be compatible with the .Net Framework.
Note; Essentially, for the immediate requirements, I intend to use an RCL predominently for creating partial views to be shared across multiple projects. I am happy to create them using C# for the project logic, and a lot of my code logic already exists in VB class libraries which I can import, but don't want to have to rewrite or relearn everything I have already done in the main bulk of my projects.
Upgrading all my work to .NetCore would be a nightmare in one go anyway...
So....
- I created a VB Web Application targeting .Net Framework 4.7.2.
- I add a reference to my RCL
- I ran a Build to check there were no version conflicts or reference incompatibilities
- I ran the website to check no build errors that weren't caught by Visual Studio
All the above worked fine, the reference added OK, no problems at all.
BUT... I cannot work out how to get the partial views in the RCL to be accessible to the website.
I am constantly getting the error
The partial view '****' was not found or no view engine supports the searched locations.
Can anyone tell me
- How to hook up a Razor Class Library in order for Partial Views to be consumed in a .Net Framework Project?
- Am I (quite likely knowing me) trying to do something that is simply not meant to work like that?!
EDIT...
Quite reasonably asked by Ben in the comments below, to clarify exactly what I'm doing
Firstly. I am literally just experimenting with a new Solution, so each time I am adding Projects I am literally just going to "Add New project > select". I am not editing the projects beyond the templates created by Visual Studio (2019 Community Edition if it makes any difference) except to add the reference to my RCl.
The RCL has a single partial view which I am trying to display, it's contents are:
@using RazorUIClassLib
<h1>This is nothing but a test</h1>
Where obviously RazorUIClassLib
is the name of the RCL... there is nothing else in it at all at this stage.
File structure is
> Pages > Shared > Test.cshtml
I have a C# .NETCore Project called WebApp1
which is the exact Project from the example downloaded from the link above has a relevant file structure
> Pages > Index.cshtml
In Index.cshtml I have added this line:
<partial name="Test"/>
(Note; Visual Studio seems a little iffy about finding the Test file as intellisense (ReSharper) is saying cannot resolve partial view'Test'
, but it runs fine)
I have a second C# .netcore project which I added to make sure there wasn't something "special" about the one I downloaded from the example code, that is structured exactly as the Visual Studio Template, and has a slightly different file structure:
Views > Home > Index.cshtml
The same single line of code is added to this file as above and works just the same (all I did after adding this brand new project was to add this line and the reference to my RCL Project)
The problem Project is a VB Project, which obviously is targeting .Net Framework NOT CORE. It is literally just the default file structure created by Visual Studio when adding a new MVC Project to the Solution, so we have
Views > Home > Index.vbhtml
Again, I have done nothing to it except to add the reference to my RCL Project and this line:
@Html.Partial("Test")
I have also tried
@Html.Partial("/Pages/Shared/Test")
@Html.Partial("~/Pages/Shared/Test")
@Html.Partial("/RazorUIClassLib/Pages/Shared/Test")
@Html.Partial("~/RazorUIClassLib/Pages/Shared/Test")
And also all the above with the .cshtml file extension.
In all cases I get varying forms of this error:
The partial view 'Test' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Test.aspx
~/Views/Home/Test.ascx
~/Views/Shared/Test.aspx
~/Views/Shared/Test.ascx
~/Views/Home/Test.cshtml
~/Views/Home/Test.vbhtml
~/Views/Shared/Test.cshtml
~/Views/Shared/Test.vbhtml
Clearly from the error it is expecting to find the Partial in the Views
Directory, but as I mention, if I add the full path I still get the same error but with different "I tried looking here" type notes.
I keep saying that I haven't changed anything to clarify that I have not made any routing alterations... am wondering if I should, but if so, not sure what!