剃刀发电机:如何使用视图在一个库编译为硕士主MVC项目中定义的局部视图 [英] Razor Generator: how to use view compiled in a library as the partial view for master defined in main mvc project

查看:251
本文介绍了剃刀发电机:如何使用视图在一个库编译为硕士主MVC项目中定义的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个ASP.NET MVC应用程序4在它周围的3000的观点。我们已经决定将这套意见分成分离的DLL和RazorGenerator编译。我们只保留主_Layout.cshtml和相关文件的主要MVC项目。

We have an ASP.NET MVC 4 application with around 3000 views in it. We've decided to split this set of views into separated DLLs and compile it with RazorGenerator. We keep only main _Layout.cshtml and related files in the main MVC project.

我们无法加载DLL从局部视图,在主MVC项目主视图在一起。详细说明如下。

We cannot load partial views from DLL together with master view in main MVC project. Detailed description is below.

什么是已经完成:


  1. 的意见成功编译成DLL文件(我已经证实,他们在二进制)

  1. The views compile successfully into DLLs (I've confirmed that they are in the binary)

创建的precompiledMvc​​Engine对象和包含的Global.asax.cs中的Application_Start使用code以下意见每个DLL注册的:

The PrecompiledMvcEngine object is created and registered for each DLL containing views using the code below in Application_Start in Global.asax.cs:

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    // ...
    // some code determining whether we've got an assembly with views
    // ...

    var engine = new PrecompiledMvcEngine(assembly);
    engine.UsePhysicalViewsIfNewer = true;

    ViewEngines.Engines.Insert(0, engine);

    // StartPage lookups are done by WebPages. 
    VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
}

什么不工作:

我无法加载主MVC项目(比如_Layout.cshtml)与在库(比如Partial.cshtml)中的一个定义的局部视图定义的视图。我用下面的code在控制器的行动告诉了MVC框架查看我要求:

I cannot load a view defined in the main MVC project (say _Layout.cshtml) with partial view defined in one of the libraries (say Partial.cshtml). I use the following code in controller's action to tell the MVC framework which view I requested:

var view = "~/Views/" + partialName + ".cshtml";    
return View(view, "~/Views/Shared/_Layout.cshtml", model);

错误消息说:
    视图〜/查看/ Partial.cshtml'或它的主人没有被发现或没有视图引擎支持搜索到的位置。在以下地点被搜查:
〜/查看/ Partial.cshtml
〜/查看/共享/ _Layout.cshtml

The error messages says: The view '~/Views/Partial.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Partial.cshtml ~/Views/Shared/_Layout.cshtml

当我尝试通过指定分装的意见之一:

When I attempt to load the views separately by specifying either:

return View("~/Views/Shared/_Layout.cshtml", model);

return View(view, model);

,右视图中找到。不过,我需要他们一起装载。在code工作时,我都需要在主MVC项目.cshtml文件。

, the right view is found. However I need them to be loaded together. The code works when I have all required .cshtml files in the main MVC project.

请注意,在编译的DLL的看法有PageVirtualPathAttribute具有相同路径在控制器的操作中指定的,例如:

Note that the views in compiled DLLs have PageVirtualPathAttribute with the same path as specified in the controller action, e.g.:

namespace SomeBaseNamespace.Views
{
    [GeneratedCode("RazorGenerator", "1.5.0.0"), PageVirtualPath("~/Views/Partial.cshtml")]
    public class Partial : WebViewPage<PartialModel>
    {
        [CompilerGenerated]
        private static class <Execute>o__SiteContainer3
        {
            // logic
        }

        public override void Execute()
        {
            // logic
        }
    }
}

要总结一下,问题是如何调用存储在主MVC项目与另一个项目中定义的部分编译视图主视图?

To sum up, the question is how to call the master view stored in main MVC project with a partial compiled view defined in another project?

推荐答案

在应用程序启动,当你的应用程序调用该行...

At app start, when your app calls this line...

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())

包含外部视图的组件可能已经尚未加载,因此不包括在视图引擎。其实我不推荐使用 AppDomain.CurrentDomain.GetAssemblies()无论如何,因为这将包括在启动时加载的所有程序集。

The assemblies containing your external views have likely not yet been loaded, and are therefore not included as view engines. I'd actually recommend against using AppDomain.CurrentDomain.GetAssemblies() anyway, as that will include all assemblies loaded at startup.

的解决方案是增加的 RazorGenerator.Mvc 的NuGet包,其中包含编译观点每一个项目。这会以类似的方式添加以下应用开始code到你...

The solution is to add the RazorGenerator.Mvc NuGet package to each project which contains compiled views. This will add the following app start code in a similar manner to yours...

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(SomeBaseNamespace.Views.RazorGeneratorMvcStart), "Start")]

namespace SomeBaseNamespace.Views
{
    public static class RazorGeneratorMvcStart
    {
        public static void Start()
        {
            var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) 
            {
                UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
            };

            ViewEngines.Engines.Insert(0, engine);
        }
    }
}

请注意这是如何创建使用当前组件的视图引擎(你的意见汇编),并把它添加到静态 ViewEngines 集合(包含在主MVC项目中)。

Note how this creates a view engine using the current assembly (your views assembly) and adds it to the static ViewEngines collection (contained within the main MVC project).

一旦投入生产,我也建议关闭 UsePhysicalViewsIfNewer 设置,增加了一个显著的性能开销。

Once in production, I'd also recommend turning off the UsePhysicalViewsIfNewer setting, which adds a significant performance overhead.

这篇关于剃刀发电机:如何使用视图在一个库编译为硕士主MVC项目中定义的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆