在的Razor视图串插? [英] String interpolation in a Razor view?

查看:141
本文介绍了在的Razor视图串插?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否支持?

如果是这样,有一些窍门能够呢?我假设剃刀不使用新的编译器足够...?在VS2015 IDE似乎与它的罚款,但在运行时我正在


  

CS1056:意外的字符$



解决方案

更新

在Visual Studio 2015年开始更新1,有在GUI做以下为您的步骤的简单过程。只需右键单击您的Web项目,然后选择启用C#6 / VB 14。更多信息可在MSDN博客文章<一href=\"http://blogs.msdn.com/b/webdev/archive/2015/12/07/new-feature-to-enable-c-6-vb-14.aspx?utm_content=buffer8a8f3&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer\">New功能,使C#6 / VB 14 。

由于这个答案是书面的,这个功能已经被添加了的NuGet包的援助。

如果您在使用本MVC5包的NuGet添加到您的解决方案。

<一个href=\"https://www.nuget.org/packages/Microsoft.$c$cDom.Providers.DotNetCompilerPlatform/\">https://www.nuget.org/packages/Microsoft.$c$cDom.Providers.DotNetCompilerPlatform/

本的NuGet包应该修改你的web.config,但检查下面的配置是在你的web.config文件(如果它不添加它):

&lt;系统codeDOM&GT;
    &LT;编译器&GT;
      &LT;编译语言=C#; CS; csharp的扩展名=CS
        键入=微软。codeDom.Providers.DotNetCompilerPlatform.CSharp codeProvider,微软,codeDom.Providers.DotNetCompilerPlatform,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35
        warningLevel =4compilerOptions =/ langversion:6 / nowarn:1659; 1699; 1701/&GT;
      &LT;编译语言=VB,VBS,VisualBasic中; VBSCRIPTVB延长=
        键入=微软。codeDom.Providers.DotNetCompilerPlatform.VB codeProvider,微软,codeDom.Providers.DotNetCompilerPlatform,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35
        warningLevel =4compilerOptions =/ langversion:14 / nowarn:41008 /定义:_MYTYPE = \\&放大器; QUOT;网络\\&放大器; QUOT; / optionInfer +/&GT;
    &LT; /编译器&GT;
  &LT; /system.$c$cdom>

在MVC6,这是内置。


原来的答复:

&LT; D​​IV&GT;
    @($你好{this.Model.SomeProperty})
&LT; / DIV&GT;

在C#6这只对MVC6。即使您正在使用的C#编译器6 MVC5,它不会工作。

诀窍是,剃刀解析器没有足够的智慧认识一些语法还,所以,必须用括号中的整个事情(你必须使用空条件操作符(时,做到这一点? 在你的剃须刀的意见),以及)。

这是说,在剃刀字符串插值在MVC6此刻有点马车,所以我不会' T表示惊讶,如果有一些问题吧。他们是否会被解决的又是另一回事。

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

解决方案

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.

这篇关于在的Razor视图串插?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆