close
Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

RazorViewEngineOptions.ParseOptions are hard to use #6009

@DamianEdwards

Description

@DamianEdwards

Trying to set the C# lang version that Razor uses via the RazorViewEngineOptions.ParseOptions is a less-than-optimal experience right now, as the Roslyn types are immutable.

The following doesn't compile as ParseOptions.LanguageVersion is not settable:

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

The IntelliSense leads you down a path where this looks like what you should do, but it doesn't work, because WithLanguageVersion returns a new instance of ParseOptions which gets lost to the ether:

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

This is actually what you need to do:

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

Or probably more correctly:

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

Not really sure we can do anything about this given the types are designed this way.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions