Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation in a Razor view?

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 '$'

like image 306
Tim Schmidt Avatar asked Jun 14 '15 17:06

Tim Schmidt


People also ask

What is string interpolation in C sharp?

An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results. This feature is available starting with C# 6.

What is string interpolation in Ruby?

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.

What is string interpolation in Swift?

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. You can use string interpolation in both single-line and multiline string literals.

What is string interpolation in Dart?

String interpolation is the process of inserting variable values into placeholders in a string literal. To concatenate strings in Dart, we can utilize string interpolation. We use the ${} symbol to implement string interpolation in your code.


2 Answers

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.

like image 58
vcsjones Avatar answered Oct 22 '22 04:10

vcsjones


  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.

like image 45
Ramin Bateni Avatar answered Oct 22 '22 06:10

Ramin Bateni