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

烈酒焚心 提交于 2019-11-27 13:54:42

问题


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

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

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.

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.

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

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

回答1:


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

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



回答2:


So I found out that there are some compilation options exposed that you call call in the 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. So I'm assuming that is necessary.

After adding Roslyn, everything compiles fine, BUT the view still gives an error.

@{
    //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?



来源:https://stackoverflow.com/questions/42844341/using-c-sharp-7-features-inside-of-a-view-in-an-asp-net-mvc-core-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!