MVC Razor分离了dll

Bar*_*xto 1 asp.net-mvc razor razorgenerator

我有两个非常相似的网站,在某些地方有很多共同点,但其他一些地方完全不同.所以我创建了三个mvc4应用程序MainSiteA,MainSiteB,SharedUI,并使用RazorGenerated在两个站点之间预编译(和共享)视图.当前的问题是我的SharedUI视图优先于MainSiteA上的编译或非编译视图,我希望它反之亦然.

有:

SiteA:
    Views/Index.cshtml (a)

SiteB:
    Views/Index.cshtml (b)
    Views/Header.cshtml (b)

SharedUI:
    Views/Index.cshtml (s)
    Views/Header.cshtml (s)
    Views/Footer.cshtml (s)
Run Code Online (Sandbox Code Playgroud)

如何以这种方式根据站点访问特定页面:

SiteA
Index.cshtml (a)
Header.cshtml (s)
Footer.cshtml (s)

SiteB
Index.cshtml (b)
Header.cshtml (b)
Footer.cshtml (s)
Run Code Online (Sandbox Code Playgroud)

我希望MVC首先查看它自己的MVC应用程序,如果找不到视图,请查看视图的共享库(SharedUI).

odi*_*erj 5

RazorGenerator.Mvc 2.1.0包含CompositePrecompiledMvc​​Engine类.如果您使用RazorGenerator在每个项目中预编译您的视图,您现在可以使用以下引擎注册站点A:

var engine = new CompositePrecompiledMvcEngine(
    PrecompiledViewAssembly.OfType<SomeSharedUIClass>(),
    PrecompiledViewAssembly.OfType<SomeSiteAClass>(
        usePhysicalViewsIfNewer: HttpContext.Current.IsDebuggingEnabled));

ViewEngines.Engines.Insert(0, engine);
VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
Run Code Online (Sandbox Code Playgroud)

和站点B的类似代码:

// ...
    PrecompiledViewAssembly.OfType<SomeSiteBClass>(
// ...
Run Code Online (Sandbox Code Playgroud)

在引擎的构造函数中注册程序集时,它会构建哈希表,其中每个元素都包含视图的虚拟路径(键)和编译的视图类型(值)之间的映射.如果此类密钥已在先前程序集中注册,则它只是使用当前程序集中的类型覆盖此映射.

因此,在SharedUI程序集注册后,哈希表将包含以下映射:

"~/Views/Index.cshtml"   -> SharedUI.Index
"~/Views/Header.cshtml"  -> SharedUI.Header
"~/Views/Footer.cshtml"  -> SharedUI.Footer
Run Code Online (Sandbox Code Playgroud)

当您放置SiteA程序集注册时,哈希表将包含:

"~/Views/Index.cshtml"   -> SiteA.Index
"~/Views/Header.cshtml"  -> SharedUI.Header
"~/Views/Footer.cshtml"  -> SharedUI.Footer
Run Code Online (Sandbox Code Playgroud)

如果你把另一个程序集与视图"〜/ 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
Run Code Online (Sandbox Code Playgroud)