简体   繁体   English

在ASP.NET MVC Core项目中使用View内部的C#7功能

[英]Using C# 7 features inside of a View in an ASP.NET MVC Core project

I've looked for other questions related to this, but none seem to be quite what I'm looking for. 我已经找到了与此相关的其他问题,但似乎没有一个我正在寻找的东西。

I have a website running on ASP.NET Core with the new project structure in VS2017. 我有一个在ASP.NET Core上运行的网站,其中包含VS2017中的新项目结构。 Code files using C#7 features compile fine. 使用C#7功能的代码文件编译正常。 But attempting to use those features in a View results in a series of errors about syntax. 但是,尝试在View中使用这些功能会导致一系列有关语法的错误。 I tried installing Roslyn to get it to be used when compiling views since from what I can tell the C#7 features are available in the Roslyn nuget package 2.x and higher. 我尝试安装Roslyn以便在编译视图时使用它,因为我可以告诉我可以在Roslyn nuget包2.x及更高版本中使用C#7功能。 But now I'm getting feedback that explicitly says 但现在我得到明确说明的反馈

error CS8059: Feature 'out variable declaration' is not available in C# 6. Please use language version 7 or greater. 错误CS8059:功能'输出变量声明'在C#6中不可用。请使用语言版本7或更高版本。

In the past I'd check the web.config, but there is no web.config in an ASP.NET Core project other than the nearly empty one at the root for handling the request off from IIS. 在过去,我会检查web.config,但是在ASP.NET核心项目中没有web.config,而是根目录中的几乎空的,用于处理来自IIS的请求。

How do I indicate that my Views should be compiled with Roslyn since that isn't done until runtime? 我如何表明我的视图应该用Roslyn编译,因为直到运行时才能完成? At least I'm assuming that would fix my problem at this point. 至少我认为这会解决我的问题。

Edit: That question is not a duplicate of this, as I mentioned at the start, I've also looked for existing questions. 编辑:这个问题并不重复,正如我在开始时提到的那样,我也在寻找现有的问题。 That's specifically enabling C#7 features in your app at compile time, and only for an ASP.NET application. 这是在编译时在您的应用程序中专门启用C#7功能,并且仅适用于ASP.NET应用程序。 I'm using ASP.NET Core, which does not have a web.config with any compilation settings defined in it. 我正在使用ASP.NET Core,它没有web.config,其中定义了任何编译设置。 Also, what I'm trying to do it for the Views which are compiled at runtime and may be on a different system. 此外,我正在尝试为运行时编译的视图执行此操作,并且可能在不同的系统上。

Solution: 解:

For anyone interested, You have to add Roslyn to your project (which I knew), but you also have to configure the RazorViewEngineOptions to use CSharpParseOptions that indicate the language version (default is 6). 对于任何感兴趣的人,您必须将Roslyn添加到您的项目(我知道),但您还必须配置RazorViewEngineOptions以使用指示语言版本的CSharpParseOptions (默认值为6)。 I had done this but I didn't do it correctly. 我做到了这一点,但我没有正确地做到这一点。 I needed to assign the result of WithLanguageVersion() back overtop of the ParseOptions to replace them. 我需要将WithLanguageVersion()的结果分配回ParseOptions以取代它们。

services.AddMvc().AddRazorOptions(options => options.ParseOptions = options.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp7));

Could you try the following (recommended by folks on the ASP.NET core team): 你可以尝试以下(由ASP.NET核心团队的人推荐):

  1. Install the Microsoft.CodeAnalysis.CSharp (version 2.0.0) and System.ValueTuple (version 4.3.0) packages 安装Microsoft.CodeAnalysis.CSharp(版本2.0.0)和System.ValueTuple(版本4.3.0)包
  2. In Startup.cs, in the ConfigureServices method, configure Razor to use C# 7 by doing the following: 在Startup.cs中,在ConfigureServices方法中,通过执行以下操作将Razor配置为使用C#7:

     services.AddMvc().AddRazorOptions(options => options.ParseOptions = new CSharpParseOptions(LanguageVersion.CSharp7)); 

So I found out that there are some compilation options exposed that you call call in the ConfigureServices() call. 所以我发现有一些编译选项暴露出你在ConfigureServices()调用中调用call。

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc().AddRazorOptions(x => x.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp7));
}

Problem is LanguageVersion.CSharp7 gives an error if you don't add Roslyn. 如果你不添加Roslyn,问题是LanguageVersion.CSharp7会出错。 So I'm assuming that is necessary. 所以我认为这是必要的。

After adding Roslyn, everything compiles fine, BUT the view still gives an error. 添加Roslyn之后,一切都编译得很好,但视图仍然会出错。

@{
    //My view code
    string s = "1";
    int.TryParse(s, out int i);
}

So if MVC exposes a RazorOptions that you can use to specify the language version, why is it not honored? 因此,如果MVC公开了一个可用于指定语言版本的RazorOptions,为什么它不被尊重?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC 在新的VS Studio 2017 ASP.NET MVC 5.2.3项目中使用C#7功能时编译错误 - Compile errors when using C# 7 features in new VS Studio 2017 ASP.NET MVC 5.2.3 project C# 和 ASP.NET MVC:在视图中使用 #if 指令 - C# and ASP.NET MVC: Using #if directive in a view c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view Blazor / C# / ASP.NET 核心 MVC:在 object 中添加一个列表 - Blazor / C# / ASP.NET Core MVC: adding a list inside an object 调用 JavaScript function 从 Z1848E732214CCE460F9B24FE0C28 视图中的 C# 代码 - Call a JavaScript function from C# code in asp.net core 5 MVC view 我的 ASP.NET Core 按钮根本不发送任何内容(ASP.NET Core MVC 和 C#) - My ASP.NET Core button not sending anything at all (ASP.NET Core MVC and C#) C# ASP.NET 核心 MVC 需要 object 参考 - C# ASP.NET Core MVC An object reference is required ASP.NET MVC视图中的C#复选框事件不在窗体内 - C# checkbox event in asp.net mvc view not inside form ASP.NET CORE MVC C# 中的 GET 和 POST 问题 - GET and POST issues in ASP.NET CORE MVC C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM