【问题标题】:Asp.net MVC Razor view - CS1525: Invalid expression term '.'Asp.net MVC Razor 视图 - CS1525:无效的表达式术语“。”
【发布时间】:2023-03-30 03:35:02
【问题描述】:

我有两个相同的 ASP.Net 4.6 MVC 项目,项目 1 在站点内使用 roslyn 编译器,运行良好。

c:\windows\system32\inetsrv>C:\Websites1\bin\roslyn\csc.exe Microsoft (R) Visual C# 编译器版本 1.2.0.60325

对于第二个项目,我收到以下错误,它使用的是 .Net 框架的编译器。

c:\windows\system32\inetsrv> "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" Microsoft (R) Visual C# 编译器版本 4.6.1590.0

Line 6: @if (!Model?.Item?.IsDerived(Templates.PageMetadata.ID) ?? true) Line 7: { Line 8: return;

c:\Website2\Views\metadata.cshtml(6,17): error CS1525: Invalid expression term '.' c:\Website2\Views\metadata.cshtml(6,18): error CS1003: Syntax error, ':' expected c:\Website2\Views\metadata.cshtml(6,23): error CS1525: Invalid expression term '.' c:\Website2\Views\metadata.cshtml(6,24): error CS1003: Syntax error, ':' expected c:\Website2\Views\metadata.cshtml(6,68): error CS1002: ; expected c:\Website2\Views\metadata.cshtml(6,68): error CS1525: Invalid expression term ')' c:\Website2\Views\metadata.cshtml(6,69): error CS1002: ; expected

我在两个项目中都使用了 Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

我的问题是为什么第二个项目不使用 roslyn 编译器,有没有办法强制它在站点内使用 roslyn 编译器?

我也尝试过 Microsoft.Net.Compilers nuget 包。

【问题讨论】:

    标签: asp.net-mvc razor roslyn c#-6.0


    【解决方案1】:

    将以下内容添加到 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.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            warningLevel="4" compilerOptions="/langversion:6 /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.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
      </system.codedom>
    

    【讨论】:

    • 请注意那里有程序集的版本号。如果安装 1.0.3,则必须将 1.0.0.0 更改为 1.0.3.0
    【解决方案2】:

    我刚刚遇到了类似的问题,并按照 Huzzi 的建议通过将标签添加到主 Web 项目的 web.config 并确保您已从 nuget 安装“Microsoft.CodeDom.Providers.DotNetCompilerPlatform”来修复它。

    【讨论】:

      【解决方案3】:

      尝试将应用程序属性中的“目标框架”值更改为更新的值。

      【讨论】:

      • 我已经尝试将其更改为 5.2.. 没有区别。
      • 我试过不同的版本,比如 4.6.1 / 5.2 没有区别。
      猜你喜欢
      • 2011-06-22
      • 2020-09-23
      • 2014-06-01
      • 2019-09-12
      • 2020-05-07
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多