I want to share the layout (Header, Navigation and Footer Razor views) across multiple ASP.NET MVC projects. How can I do that?
Can I create a custom NuGet package to wrap the common Razor files, images and CSS?
Razor Pages is sometimes described as implementing the MVVM (Model, View ViewModel) pattern. It doesn't. The MVVM pattern is applied to applications where the presentation and model share the same layer. It is popular in WPF, mobile application development, and some JavaScript libraries.
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder.
RCL Pages layoutRazor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains.
I've hit this more than once via Google now, and though I'd pose another solution (which, IMO, is much safer and more manageable than using precompilers and packages).
In fact, I've shamelessly scraped verbatim an answer from Erik Phillips, so please, credit to him.
what he said... (he says this for javascript files in particular, but this works just as well with any other types) source answer
Here is what I would recommend:
Right click the solution and create a New Solution Folder called Common Javascript Files
(or whatever you feel like calling it.
Right click on the Solution, click Open Folder in Windows Explorer, or navigate there manually for other versions of Visual Studio :(
In the solution directory, create a directory with the same name as the solution folder (solution folders do not normally match directories at the source code level but this will for sanity sake).
In this new directory, add files that need to be shared between solutions.
In Visual Studio, click the solution folder and select Add - Existing Item.
In the file selection dialog, navigate to the directory previous created, select the file(s) added to the directory and click Add.
In each Project that needs a shared file, right click on the project (or directory within the project) and click Add - Existing Item.
Navigate to the shared Directory, Select the files and click the drop down arrow then click Add As Link.
Now the files in the projects are essentially short cuts to the files in the Solution Folder. But they are treated as actual files in the project (this includes .CS or Visual Basic files, they will be compiled as files that actually exist in the project).
PROS
CONS
AND - stealing from one more answer to consolidate info, here's how you get these to output for debugging (taken from comment below the scraped answer, link to blog post for build target for output for debugging)
Add this to each consuming project's .csproj
file:
<Target Name="CopyLinkedContentFiles" BeforeTargets="Build"> <Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles='true' OverwriteReadOnlyFiles='true' Condition="'%(Content.Link)' != ''" /> </Target>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With