gpt4 book ai didi

asp.net - Razor & null 传播 - 在显式 C# 6 MVC 5 项目下不工作

转载 作者:行者123 更新时间:2023-12-02 11:21:49 24 4
gpt4 key购买 nike

当前的项目:

  • ASP.NET 4.6.2
  • MVC 5
  • Visual Studio 2015 社区 v14.0.25431.01 更新 3
  • 安装了CodeDOM Providers for .NET Compiler通过项目安装 nuget 包 -> 安装 C# 6
  • 确认我确实有 compiler element在我的 Web.Config 中,它引用了 C# 6

  • 我的 Razor 代码:
    @if(Session["Type"]?.ToString() == "Insurance") {
    <text>policy of insurance</text>
    } else if(Session["Type"]?.ToString() == "Warranty") {
    <text>policy of warranty</text>
    } else {
    <text>protection policy</text>
    }

    Visual Studio 明确地将此标记为错误,

    Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater.



    Visual Studio 明确使用 C# 6:

    enter image description here

    当我尝试运行页面本身时,出现错误:

    CS1525: Invalid expression term '.'



    这表明它明确地在问号后面的句点之后。

    这个项目明确作为一个 C# 6 项目开始,我很困惑为什么它重新回到 C#5。

    另一个奇怪的问题: 这在调试和我的本地测试设置中都运行良好 (我发布到文件系统,使用本地 IIS 查看),但是当我上传到服务器(有很多很多其他的 C#6 站点)时,它就崩溃了。这是我遇到的第一个真正引发如此严重错误的 Razor 方面的怪癖。

    最佳答案

    我遇到了类似的问题,发现我的 Web.config 文件中缺少以下内容:

    <system.codedom>
    <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
    </system.codedom>

    关于asp.net - Razor & null 传播 - 在显式 C# 6 MVC 5 项目下不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401851/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com