spark view precompile

来源:互联网 发布:千千静听软件 编辑:程序博客网 时间:2024/05/02 04:51


只要是看E文来的,不管是否加入了整理编排,统统算translation...

原文:http://sparkviewengine.com/documentation/precompiling  不仅限于此文

view descriptors and a SparkViewEngine/

batch descriptor and a SparkViewFactory/

方式一:

Global Application_Start调用以下方法-- 其实不推荐使用在application_start运行如此heavy的代码

 public static void PrecompileViews(ControllerBuilder builder)        {            var controllerFactory = (SparkControllerFactory)builder.GetControllerFactory();            var viewFactory = new SparkViewFactory(controllerFactory.Settings);            var batch = new SparkBatchDescriptor();            batch                .For<HomeController>()                .For<ProductsController>();            viewFactory.Precompile(batch);        }

方式二:

            var engine = new SparkViewEngine(settings)                             {                                 ViewFolder = new FileSystemViewFolder(viewsLocation)                             };             engine.BatchCompilation(targetPath, Global.AllKnownDescriptors());

方法三:这个也是在之前的MVC 多租户中使用到的方式:Precompiling bin-deployable assembly with installutil and with a post-build step

这可能是最酷的方式了,尤其是你开发的web app要部署到一个medium信任共享的环境中,动态产生view dll会存在问题。

我们看一下,如果在web.config中有这个Trust level="Medium",如果web app尝试在运行时编译view,app会抛出exception",接下来我们看看应该怎么做:

1。在toolbox中添加spark.mvc.... precompileInstaller

(If it's not in the toolbox you may need to right-click one of the categories, say "Choose Items...", and Browse... to locate the assembly that contains the tool you want.)

2。在项目中添加installer class,会产生一个设计页面,把precompilerInstaller拖放上去。

3。选中precompileInstaller1,如果愿意,可以在属性中设置target assembly的文件名,默认是assembly+.view//在属性page的events中双击describeBatch。然后添加实现:

private void precompileInstaller1_DescribeBatch(object sender, DescribeBatchEventArgs e)        {            e.Batch                .For<HomeController>()                .For<HomeController>().Layout("Ajax").Include("_Notification")                .For<AccountController>();        }

4。第4件事也是最后一件事情,在项目的post build event commandline中输入下面的命令

%systemroot%\Microsoft.NET\Framework\v2.0.50727\installutil "$(TargetPath)"

note:请注意修改.net framework的版本