打印和/或修改 razor 编译器服务用来编译 cshtml 的 c# 版本 [英] Print and/or modify the c# version that the razor compiler service uses to compile cshtml

查看:38
本文介绍了打印和/或修改 razor 编译器服务用来编译 cshtml 的 c# 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够找出 razor 用于编译我的 cshtml 模板的 C# 版本.我想要这个的原因是 这个重大变化.

I'd like to be able to find out which C# version razor uses to compile my cshtml templates. The reason why I want this, is this breaking change.

我们在 foreach 语句中有一个 lambda,它在我们的本地开发机器上运行良好,但在我们的测试环境(没有安装 C# 5)上产生了一个错误.这个错误非常难以调试(我们甚至复制了所有测试环境的 DLL 和数据库,但仍然无法重现该错误).

We had a lambda in a foreach statement that worked fine on our local dev machines but produced a bug on our test environment (which doesn't have C# 5 installed). This bug was VERY hard to debug (we even copied all the test environment DLLs and databases and were still not able to reproduce the bug).

因此,为了防止将来出现这种开发/测试差异,我想知道是否有办法指定 razor 用于编译 cshtml 文件的 C# 版本.如果我能检查 razor 使用的 C# 版本(通过打印它)也很好.

So to prevent this dev/test difference in the future I would like to know if there's a way to specify the C# version that razor should be using to compile cshtml files. It would also be nice if I could check the C# version that razor uses (by printing it).

更新:根据要求,提供有关此行为如何发生的更多详细信息.
我们使用 devexpress mvc 网格在剃刀视图中显示数据.为了以动态方式添加列,我们循环(foreach)一个列表,该列表在数据网格中插入列(使用 lambda).一个简化的例子:

Update: As requested, more details on how this behavior occurred.
We use a devexpress mvc grid to display data in our razor views. To add columns in a dynamic way we loop (foreach) a list which inserts columns in the datagrid (using a lambda). A simplified example:

@Html.DevExpress().GridView(
    settings =>
    {
        settings.Name = "gvDashboard";
        //Some more settings

        settings.Columns.Add(column =>
        {
            column.FieldName = Model.DashboardItems.PropertyName(p => p.Id);
            column.Caption = "Id";
            //Some more column settings
        });

        foreach (var extraColumnLoopVar in Model.ExtraColumns)
        {
            //We added this to solve the problem
            var extraColumn = extraColumnLoopVar; 

            settings.Columns.Add(column =>
            {
                column.Caption = extraColumn.Name;
                //Some more column settings

                column.SetDataItemTemplateContent(content =>
                {
                    Html.ViewContext.Writer.Write(extraColumn.MyValue);
                });
            });
        }
    });

推荐答案

razor 的版本在 Views 目录中的 Web.config 文件中指定.它必须与 System.Web.WebPages 程序集的依赖程序集列表中的版本之一匹配.此条目位于主 Web.config 文件中(通常位于应用程序树的根部)

The version of razor is specified on the Web.config file within the Views directory. It has to match one of the versions on the dependent assemblies list for the System.Web.WebPages assembly. This entry is on the main Web.config file (usually located at the root of your application tree)

从配置文件中检索数据相当简单.请参阅 ConfigurationManager为此类.如果您想在运行时执行此操作.

Retrieving data from config files is fairly simple. See the ConfigurationManager class for this. If you'd like to do it at runtime.

还可以根据应用程序的引用程序集确定 Razor 版本.您可以为此使用反射,这里有一个片段,它会吐出所有引用的程序集:

It's also possible to determine the Razor version based on the referenced assemblies of your application. You could use reflection for that, here's a snippet that spits out all the referenced assemblies:

var sb = new StringBuilder();
Assembly asm = Assembly.GetExecutingAssembly();
sb.AppendLine("File Version:");
sb.AppendLine(asm.FullName);

sb.AppendLine("References :");
AssemblyName[] asmNames = asm.GetReferencedAssemblies();
foreach (AssemblyName nm in asmNames)
{
    sb.AppendLine(nm.FullName);
}

// use sb.ToString() to print out wherever you need to

显然,您选择在运行时评估此信息的方法可能会对性能产生影响.

Obviously there might be performance implications based of the method you choose to evaluate this info at runtime.

更新 1

从下面的评论中,我认为当您提到编译时,您指的是编译时的 Razor 视图解析过程.Razor View Compilation"还有其他概念,请参阅Razor Generator,无论您的意思是两者都依赖于哪个对 System.Web.WebPages 程序集的引用,其中包含 Razor 视图引擎本身的库依赖项.因此,如果您知道您指向的是哪个程序集,您就知道您使用的是哪个版本的 Razor.

From the comments below I take that when you mention compilation you refer to the Razor view parsing process at compile time. There are other concepts of "Razor View Compilation" see Razor Generator, regardless of which one you do mean both rely on the reference to the System.Web.WebPages assembly which contains the library dependencies for the Razor View Engine itself. So if you know which assembly you are pointing at, you know which version of Razor you are using.

更新 2

考虑到您担心与视图中使用的 C# 版本发生冲突,您应该使用以下经验法则:您应该始终引用面向框架的 DLL (System.Web.WebPages)您正在使用.重要的是要记住,MVC 框架与语言本身有不同的更新时间表.一个很好的例子是 async 关键字,它首先被添加到语言中,后来被 MVC 框架采用.通常新版本的 .NET Framework 向后兼容直到 2.0 版,当你使用不推荐使用的东西时,你会收到编译警告.如果您想在编译时使用旧版本的框架,您可以随时更改 IDE 上的目标框架.

Taking into account that you are worried about conflicts with the version of C# that you use in your views, you should use the following rule of thumb: You should always reference the DLL (System.Web.WebPages) that targets the framework you are using. It's important to remember that the MVC framework has a different update timeline than the language itself. A good example is the async keyword, it was added to the language first and later adopted by the MVC framework. Usually new versions of the .NET Framework are backwards compatible 'til version 2.0 and when you use deprecated stuff you get compilation warnings. If you'd like to use older versions of the framework while compiling you can always resort to changing the target framework on your IDE.

除了使您的引用和目标框架正确之外,请记住,当您在 IIS 中设置 Web 应用程序时,您指定了一个与框架版本相关联的应用程序池.您可能正在使用 .NET 框架的新功能,并且您可能希望它们能够工作,因为您安装了新版本,但您的应用程序正在不同版本的应用程序池中运行.

In addition to getting your references and target framework right, remember that when you setup Web applications in IIS you specify an application pool tied to the framework version. You might be using new features of the .NET framework, and you might expect for them to work because you have the new version installed, but your application is running in an application pool of a different version.

在 DLL 和 DLL 之间进行这种令人讨厌的混淆之前框架版本 我会遵循升级指南,因为我在以前的版本中实现的一些代码可能无法在新版本中正常工作.

Before having this nasty mixup between DLL & Frameworks versions I'd follow upgrade guidelines taking into account that some of the code I implemented with the previous version might not work properly with the new one.

更新 3

这里有一些代码,用于在运行时检索您的 CLR 版本,如 MSDN.

Here's some code to retrieve your CLR version at Runtime as described in MSDN.

// Get the common language runtime version.
Version ver = Environment.Version;
Console.WriteLine("CLR Version {0}", ver.ToString());

这篇关于打印和/或修改 razor 编译器服务用来编译 cshtml 的 c# 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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