c# - String interpolation in a Razor view? -
is supported?
if so, there trick enabling it? i'm assuming razor isn't using new enough compiler...? vs2015 ide seems fine @ runtime getting
cs1056: unexpected character '$'
update:
starting in visual studio 2015 update 1, there simple process in gui steps below you. right-click web project , select "enable c# 6 / vb 14". more information available on msdn blog post, "new feature enable c# 6 / vb 14".
since answer written, functionality has been added assistance of nuget package.
add nuget package solution if using mvc5.
https://www.nuget.org/packages/microsoft.codedom.providers.dotnetcompilerplatform/
the nuget package should modify web.config, check following configuration in web.config file (and if isn't add 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=\"web\" /optioninfer+"/> </compilers> </system.codedom>
in mvc6, built-in.
original answer:
<div> @($"hello {this.model.someproperty}") </div>
this works in c# 6 mvc6. if running mvc5 c# 6 compiler, won't work.
the trick razor parser not smart enough recognize syntaxes yet, must wrap whole thing in parentheses (you must when using null-conditional operator (?.
) in razor views well).
that said, string interpolation in razor a bit buggy @ moment in mvc6, wouldn't surprised if there issues it. whether or not addressed matter.
Comments
Post a Comment