104

Is this supported?

If so, is there some trick to enabling it? I'm assuming Razor isn't using a new enough compiler...? The VS2015 IDE seems to be fine with it but at runtime I am getting

CS1056: Unexpected character '$'

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Tim Schmidt
  • 1,855
  • 3
  • 21
  • 21
  • Can you show the code you're talking about? – Jeroen Vannevel Jun 14 '15 at 17:56
  • Which ASP.NET are you using (ASP.NET 5: the new one built on .NET Core; or the update to ASP.NET 4)? – Richard Jun 14 '15 at 18:00
  • 1
    Thank you! I'm glad someone else said that. I thought it was pretty helpful and I personally know 2 people that had the same problem with the same solution. Who the heck is in charge here anyway?? – Tim Schmidt Jul 19 '15 at 16:20
  • @TimSchmidt The close queue is long, so sometimes questions get closed inappropriately. But the reopen queue is short, so if it really should be reopened, it will be fairly quickly. – o11c Jul 21 '15 at 20:48

3 Answers3

148

Update:

Starting in Visual Studio 2015 Update 1, there is a simple process in the GUI to do the steps below for you. Simply right-click your web project and select "Enable C# 6 / VB 14". More information is available on the MSDN blog post, "New feature to enable C# 6 / VB 14".

Since this answer was written, this functionality has been added with the assistance of a NuGet package.

Add this Nuget package to your solution if you are using MVC5.

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

The nuget package should modify your web.config, but check that the following configuration is in your web.config file (and if it isn't add it in):

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /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.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

In MVC6, this is built-in.


Original answer:

<div>
    @($"Hello {this.Model.SomeProperty}")
</div>

This only works in C# 6 with MVC6. Even if you are running MVC5 with the C# 6 compiler, it won't work.

The trick is that the razor parser is not smart enough to recognize some syntaxes yet, so you must wrap the whole thing in parentheses (you must do this when using the null-conditional operator (?.) in your razor views as well).

That said, string interpolation in Razor is a bit buggy at the moment in MVC6, so I wouldn't be surprised if there were some issues with it. whether or not they will be addressed is another matter.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Ah, bingo, I was using MVC 5. Thanks! – Tim Schmidt Jun 14 '15 at 21:12
  • 5
    MVC 5.2.3 does work, you just have to add the rosyln code dom package for asp.net. See this answer in a non closed question: http://stackoverflow.com/a/31548221/637783 – jbtule Jul 21 '15 at 19:49
  • 1
    by the way vs updated the error and now i says that c#6 is not supported, which is better – CMS Aug 07 '15 at 14:24
  • Although I understand that Razor's intuitive switch between markup and C# does not work seamlessly with the $, I think the compiler version has nothing to do with this, that's why my wording in my question. Once Razor recognizes it's C# it goes to the generated C# class. Then it's entirely up the the configured compiler it understands or gives an error. – g.pickardou Aug 14 '15 at 14:30
  • @CMS: I have no updated error message. Compiler simply drops the spoon at the $ sign – g.pickardou Aug 14 '15 at 14:32
  • This is not true, there is a NuGet package which allows MVC 5 to use the new Roslyn compiler for runtime: http://dusted.codes/using-csharp-6-features-in-aspdotnet-mvc-5-razor-views – dustinmoris Oct 09 '15 at 09:23
  • @vcsjones sorry, I meant null propagation operator – Ody Mar 07 '16 at 15:17
  • 2
    Even after installing this NuGet package which updated the Web.Config too, I am still getting the following error message as a tool tip: "Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater." – Zeshan Munir May 05 '16 at 17:46
  • 1
    I didn't have "Enable C# 6 / VB 14" option on right-click menu. But I had it in Visual Studio, Project menu so I've enabled it from there. Thanks. – HasanG Mar 26 '17 at 01:14
  • How would I go about doing this if I have no web.config, but instead have an app.config for Service Fabric? – claudekennilol Apr 06 '17 at 12:33
  • This works with MVC 5.2.3, .Net 4.7, and the latest version of the nuget package (2.0.1) in the answer. – TSmith Mar 28 '19 at 13:42
14
  1. Run the following command in the Package Manager Console to add a required CodeDom provider to your project. It will modify your web.config file automatically to add CodeDom required settings to it.

    Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

  2. Restart Visual Studio

Note: As @Jake mentioned in his comment under this answer, if you have the DotNetCompilerPlatform package already it may just need to be updated.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
  • This is the correct solution today. From Microsoft's description: `Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs. This provides support for new language features in systems using CodeDOM (e.g. ASP.NET runtime compilation) as well as improving the compilation performance of these systems.` – Luis Ferrao Jan 23 '17 at 14:34
  • 1
    For my scenario, I had this package already, but it was version 1.0.5. Updating to 1.0.8 solved the issue. – Jake Mar 08 '18 at 23:03
  • Restarting Visual Studio wasn't necessary for me. – Johnathan Barclay Jul 17 '18 at 07:39
  • @JohnathanBarclay the restart was necessary in my case. I think it's safe to assume that it will be necessary for some people. – Loudenvier Jun 08 '20 at 13:18
0

If you want to do:

<div>
    @($"Hello {this.Model.SomeProperty}")
</div>

Simply you can do:

<div>
    "Hello @this.Model.SomeProperty"
</div>

This work with MVC5