[英]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核心团队的人推荐):
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.