Razor 视图中的字符串插值? [英] String interpolation in a Razor view?

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

问题描述

支持吗?

如果是这样,是否有一些技巧可以启用它?我假设 Razor 没有使用足够新的编译器......?VS2015 IDE 似乎没问题,但在运行时我得到了

<块引用>

CS1056:意外字符$"

解决方案

更新:

从 Visual Studio 2015 Update 1 开始,GUI 中有一个简单的过程可以为您执行以下步骤.只需右键单击您的 Web 项目并选择启用 C# 6/VB 14".更多信息可在 MSDN 博客文章启用 C# 6/VB 14 的新功能".

自从编写此答案以来,已在 NuGet 包的帮助下添加了此功能.

如果您使用的是 MVC5,请将此 Nuget 包添加到您的解决方案中.

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

nuget 包应该修改您的 web.config,但请检查以下配置是否在您的 web.config 文件中(如果未添加):

 <编译器><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&quot;/optionInfer+"/></编译器></system.codedom>

在 MVC6 中,这是内置的.

<小时>

原答案:

@($"你好 {this.Model.SomeProperty}")

这仅适用于带有 MVC6 的 C# 6.即使您使用 C# 6 编译器运行 MVC5,它也不会工作.

诀窍在于,razor 解析器还不够智能,无法识别某些语法,因此您必须将整个内容括在括号中(在使用空条件运算符 (?.) 在您的剃刀视图中).

也就是说,Razor 中的字符串插值目前在 MVC6 中有点问题,所以我不会如果它有一些问题,请不要感到惊讶.他们是否会得到解决是另一回事.

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天全站免登陆