zoukankan      html  css  js  c++  java
  • Razor字符串处理

    需要注意的是低版本是不支持C# 6语法中的string interpolation的

      <label>
                                @if (!string.IsNullOrEmpty(Model.BudgetValueUpdatedBy))
                                {
                                    @(Model.BudgetValueUpdatedBy + " " + Model.BudgetValueUpdatedOn)
                                }
                                else
                                {
                                    @("Chuck hello")
                                }
                            </label>
                            <label>Chuck Test</label>

    String interpolation in a Razor view?

    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.

    旧版本不支持,可以考虑使用codedom来支持。

  • 相关阅读:
    树的子结构(剑指offer_26)
    合并两个排序的链表(剑指offer_25)
    反转链表(剑指offer_24)多看多思多想
    链表中环的入口结点(剑指offer_23)
    链表中倒数第k个节点
    调整数组顺序使奇数位于偶数前面(剑指offer_21)
    表示数值的字符串(剑指offer_20)
    1676. 跳石头
    1670. 回合制游戏
    1667. 区间统计(回顾)
  • 原文地址:https://www.cnblogs.com/chucklu/p/11400231.html
Copyright © 2011-2022 走看看