Sharing Views Between Projects

ASP.NET MVC makes it very easy to share views within the same website. Multiple controller actions can reference the same view when the shared view lives in the same folder as the other views for that controller or in the website’s Shared folder. But how do you share views across projects?

Out of the box, there is no way to create a view in one project and reuse it in other projects. However, as you discovered in Chapter 5, Razor views are really just fancy designers that eventually generate .NET code, which can be compiled into assemblies, and compiled assemblies are certainly reusable across projects! Thus, what you need is a tool that can take the Razor views that you author in a separate project and run the Razor API directly against them to generate .NET code. Though this book gives you all the information you need to build a tool, the good news is that you don’t have to: some folks in the open source community have already created it for you!

The Razor Single File Generator

The Razor Single File Generator is an open source Visual Studio extension that allows you to create reusable Razor views that aren’t tied to any specific website. The extension lets you use Razor templates to generate classes much like the Visual Studio Settings designer and Windows Forms designer generate settings classes and Windows Forms classes.

Installing the Razor Single File Generator

Though the complete source code is hosted on CodePlex, the Razor Single File Generator ...

Get Programming Razor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.