ASP.NET MVC-在生产环境中实时更新预编译的Razor View文件 [英] ASP.NET MVC - Update Pre-compiled Razor View file Live in Production

查看:213
本文介绍了ASP.NET MVC-在生产环境中实时更新预编译的Razor View文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下可能性:

  1. 通过在Visual Studio中打开项目设置,使用我们的MVC应用程序预编译Razor视图.
  2. 将应用程序部署到生产环境.
  3. 然后在以后的阶段中,通过覆盖生产中的现有* .cshtml文件来更新视图,而无需回收应用程序池或重新编译项目并重新部署构建.?

推荐答案

是的,您可以这样做.视图引擎将把每个目录中的所有文件重新编译成一个单独的DLL,变成这样的文件

Yes you can do this. The view engine will recompile all the files in each directory into a separate DLL into a file like this

C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \临时ASP.NET 文件\ micropedi_mvc \ d3b5b4cd \ eb219373 \ App_global.asax.ihdiifed.DLL

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\micropedi_mvc\d3b5b4cd\eb219373\App_global.asax.ihdiifed.DLL

您可以使用ILSpyReflector打开此文件,并确切查看编译到该文件的文件.如果您有许多目录,则会看到生成了几个DLL.

You can use ILSpy or Reflector to open up this file and see exactly which files were compiled to it. if you have many directories you'll see several DLLs generated.

Note that all that MvcBuildViews`会运行此生成后事件,它的作用与您点击应用程序所需的每个文件的作用相同.

Note that all thatMvcBuildViews` does is run this post build event which just does the same as if you were to hit each file needed for your application.

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
   <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

为获得最佳性能,我建议

For maximum performance I'd recommend the RazorGenerator precompiler. This actually embeds the cshtml files into your main DLL which isn't what MvcBuildViews does at all. Plus its much faster.

如果要在生产环境中使用此功能,则需要将RazorGeneratorMvcStart.cs中的UsePhysicalViewsIfNewer更改为true,以便在更新视图时重新编译.

If you wanted to use this in production you'd need to change UsePhysicalViewsIfNewer in RazorGeneratorMvcStart.cs to true so that it would recompile if you updated the view.

提示:如果您想确切地找到cshtml文件被编译为将其添加到页面中的DLL

Tip: If you ever want to find exactly which DLL the cshtml file is compiled to just add this to your page

 <div>This file is compiled to  @this.GetType().Assembly.CodeBase</div> 

然后,您无需查找即可获得完整路径.

Then you'll get the full path without looking for it.

这篇关于ASP.NET MVC-在生产环境中实时更新预编译的Razor View文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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