在ASP.NET MVC Core项目的视图中使用C#7功能 [英] Using C# 7 features inside of a View in an ASP.NET MVC Core project

查看:170
本文介绍了在ASP.NET MVC Core项目的视图中使用C#7功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找与此相关的其他问题,但似乎没有一个完全符合我的需求.

I've looked for other questions related to this, but none seem to be quite what I'm looking for.

我有一个在ASP.NET Core上运行的网站,该网站具有VS2017中的新项目结构.使用C#7功能的代码文件可以很好地编译.但是,尝试在View中使用这些功能会导致一系列有关语法的错误.我尝试安装Roslyn,以便在编译视图时使用它,因为从我可以知道的情况来看,Roslyn nuget软件包2.x和更高版本中提供了C#7功能.但是,现在我收到了明确指出

I have a website running on ASP.NET Core with the new project structure in VS2017. Code files using C#7 features compile fine. But attempting to use those features in a View results in a series of errors about syntax. 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. But now I'm getting feedback that explicitly says

错误CS8059:C#6中没有功能输出变量声明".请使用语言版本7或更高版本.

error CS8059: Feature 'out variable declaration' is not available in C# 6. Please use language version 7 or greater.

过去我会检查web.config,但是ASP.NET Core项目中除了用于处理来自IIS的请求的根目录中几乎没有空的项目外,没有其他web.config.

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.

我该如何指示我的视图应使用Roslyn进行编译,因为这要等到运行时才能完成?至少我认为这会解决我的问题.

How do I indicate that my Views should be compiled with Roslyn since that isn't done until runtime? At least I'm assuming that would fix my problem at this point.

正如我在开始时提到的那样,这个问题不是重复的,我也一直在寻找现有的问题.这是专门在编译时在应用程序中启用C#7功能的功能,并且仅适用于ASP.NET应用程序.我正在使用ASP.NET Core,它没有定义任何编译设置的web.config.另外,我正在尝试针对运行时编译的View进行操作,这些View可能在其他系统上.

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. I'm using ASP.NET Core, which does not have a web.config with any compilation settings defined in it. Also, what I'm trying to do it for the Views which are compiled at runtime and may be on a different system.

解决方案:

对于感兴趣的任何人,您都必须将Roslyn添加到您的项目中(我知道),但是还必须配置RazorViewEngineOptions以使用CSharpParseOptions来指示语言版本(默认为6).我已经做到了,但是没有正确做.我需要将WithLanguageVersion()的结果分配回ParseOptions的顶部以替换它们.

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). 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.

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

推荐答案

您可以尝试以下方法(由ASP.NET核心团队的人员推荐):

Could you try the following (recommended by folks on the ASP.NET core team):

  1. 安装Microsoft.CodeAnalysis.CSharp(版本2.0.0)和System.ValueTuple(版本4.3.0)程序包
  2. 在Startup.cs的ConfigureServices方法中,通过执行以下操作将Razor配置为使用C#7:

  1. Install the Microsoft.CodeAnalysis.CSharp (version 2.0.0) and System.ValueTuple (version 4.3.0) packages
  2. In Startup.cs, in the ConfigureServices method, configure Razor to use C# 7 by doing the following:

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

这篇关于在ASP.NET MVC Core项目的视图中使用C#7功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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