Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor & null propagation - not working under explicit C# 6 MVC 5 project

Current project:

  • ASP.NET 4.6.2
  • MVC 5
  • Visual Studio 2015 Community v14.0.25431.01 Update 3
  • Installed the CodeDOM Providers for .NET Compiler nuget package via Project -> Install C# 6
  • Confirmed that I do have the compiler element in my Web.Config and that it is referencing C# 6

My Razor code:

@if(Session["Type"]?.ToString() == "Insurance") {
  <text>policy of insurance</text>
} else if(Session["Type"]?.ToString() == "Warranty") {
  <text>policy of warranty</text>
} else {
  <text>protection policy</text>
}

Visual Studio explicitly flags this with an error,

Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater.

And Visual Studio is explicitly using C# 6:

enter image description here

When I try to run the page itself, I get an error:

CS1525: Invalid expression term '.'

which shows that it is explicitly going after the period following the question mark.

This project was explicitly started as a C# 6 project, and I am quite confused as to why it is derping back to C#5.

Another strange issue: this is working just fine both in debug as well as on my local test setup (I publish to the filesystem, view using local IIS), but when I upload to the server (which has many, many other C#6 sites) it craps out. This is the first razor-side quirk I have ever run across that actually throws an error this badly.

like image 229
René Kåbis Avatar asked Jun 07 '17 00:06

René Kåbis


People also ask

Is it called a razor or a shaver?

So what's the difference? When we say electric shaver, we're referring to either a foil or rotary shaver, and when we say razor, we mean either a cartridge razor or a safety razor.

Which type of razor is best?

For the fastest and easiest shave, an electric razor will likely be the best bet, and some of our experts say that using one can also reduce the occurrence of ingrown hairs. “For those who don't like the traditional methods, I recommend foil shavers,” says Langevin.

What is razor used for?

Razor Basics Shaving is simply using a razor to remove the tip of the hair shaft that has grown up through the skin. Razors come in a bunch of different forms. There are standard razors that are either completely disposable or have a disposable blade that needs to be replaced regularly, and there are electric razors.

Is razor safe for shaving?

The benefits of safety razor shaveThat sharp blade is flush against your skin. So, be careful, but if you master the craft, you'll never look back. A single blade means less dragging across the face—and less chance that your top layer of skin will come off with the hairs.


1 Answers

I was having a similar problem and found that I was missing the following from my Web.config file:

<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
</system.codedom>
like image 180
Abbie 'Scarophion' deZ Avatar answered Oct 10 '22 16:10

Abbie 'Scarophion' deZ