RazorGenerator.Mvc 2.1.0 包括 CompositePrecompiledMvcEngine 类。如果您使用 RazorGenerator 在每个项目中预编译您的视图,您现在可以为站点 A 使用以下引擎注册:
var engine = new CompositePrecompiledMvcEngine(
PrecompiledViewAssembly.OfType<SomeSharedUIClass>(),
PrecompiledViewAssembly.OfType<SomeSiteAClass>(
usePhysicalViewsIfNewer: HttpContext.Current.IsDebuggingEnabled));
ViewEngines.Engines.Insert(0, engine);
VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
站点 B 的类似代码:
// ...
PrecompiledViewAssembly.OfType<SomeSiteBClass>(
// ...
当您在引擎的构造函数中注册程序集时,它会构建哈希表,其中每个元素都包含视图的虚拟路径(键)和编译视图类型(值)之间的映射。如果这样的键已经注册到以前的程序集之一,它只是用当前程序集的类型覆盖这个映射。
因此,在 SharedUI 程序集注册之后,哈希表将包含以下映射:
"~/Views/Index.cshtml" -> SharedUI.Index
"~/Views/Header.cshtml" -> SharedUI.Header
"~/Views/Footer.cshtml" -> SharedUI.Footer
当您将 SiteA 程序集注册时,哈希表将包含:
"~/Views/Index.cshtml" -> SiteA.Index
"~/Views/Header.cshtml" -> SharedUI.Header
"~/Views/Footer.cshtml" -> SharedUI.Footer
如果您放置另一个带有视图“~/Views/Footer.cshtml”和“~/Views/Sidebar.cshtml”的程序集,哈希表将包含:
"~/Views/Index.cshtml" -> SiteA.Index
"~/Views/Header.cshtml" -> SharedUI.Header
"~/Views/Footer.cshtml" -> Another.Footer
"~/Views/Sidebar.cshtml" -> Another.Sidebar