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=\"Web\" /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来支持。
Razor字符串处理的更多相关文章
- .NET Core使用FluentEmail发送邮件
前言 在实际的项目开发中,我们会遇到许多需要通过程序发送邮件的场景,比如异常报警.消息.进度通知等等.一般情况下我们使用原生的SmtpClient类库居多,它能满足我们绝大多数场景.但是使用起来不够简 ...
- razor 拼接字符串
在asp.net引擎中 拼接字符串可以这样写 <script src="~/script/<%=scriptname%>.js"></script&g ...
- .net mvc RazorEngine 字符串razor参数替换
在.net中有一个比较好的字符串参数替换的方案RazorEngine推荐大家看看原网站,然后做个小联系然后你就懂啦 首先呢得下载一个吧, vs中tools-> Library Paging Ma ...
- ASP.NET Core中使用Razor视图引擎渲染视图为字符串(转)
一.视图渲染说明 在有些项目需求上或许需要根据模板生产静态页面,那么你一样可以用Razor语法去直接解析你的页面从而把解析的页面生成静态页,这样的使用场景很多,不限于生成静态页面,视图引擎为我们提供了 ...
- ASP.NET MVC Razor 输出没有编码的HTML字符串
Razor引擎之前要输出一段没有编码的字符串,只要@加变量名就可以了,Razor却不能这样,感觉是有点麻烦. 在Razor Beta 2以前的版本可以: @(new HtmlString(mystri ...
- Core中使用Razor视图引擎渲染视图为字符串 阅读目录
Core中使用Razor视图引擎渲染视图为字符串 } <!DOCTYPE html> <html> <head> <title>Render view ...
- Asp.net MVC Razor输出字符串方法(js中嵌入razor)
@{ Model p = new Model(); //输出名称和年龄 //1.第一种方式 @:姓名=@p.Name //2.第二中方式 <text>年龄=</text>p.A ...
- webpages框架使用@razor语法向js代码传递Json字符串
进入web开发时间太短,一个人尝试着做了几个初级项目,遇到了太多的困难.尽管不是学开发专业的,仅为爱好所以硬着头皮坚持了下来. 将遇到的问题记录下来,备查. 使用vs2015中asp.net razo ...
- ASP.NET Core中使用Razor视图引擎渲染视图为字符串
一.前言 在有些项目需求上或许需要根据模板生产静态页面,那么你一样可以用Razor语法去直接解析你的页面从而把解析的页面生成静态页,这样的使用场景很多,不限于生成静态页面,视图引擎为我们提供了模型到视 ...
随机推荐
- 使用帅气的cordic算法进行坐标系互转及log10的求解
参考博客 https://blog.csdn.net/u010712012/article/details/77755567 https://blog.csdn.net/Reborn_Lee/arti ...
- S2-019、S2-020
前言 “Struts2系列起始篇”是我整各系列的核心,希望大家能花些时间先看看. 正文 我发现关于一些早期的Struts2的漏洞,网上的分析文章并不多,不知道是不是我打开浏览器的方式不对,唯一看到的两 ...
- Oracle学习笔记——Linux下开启Oracle
1.开启数据库 sqlplus / as sysdba startup 2.启动监听:lsnrctl start; 查看监听状态:lsnrctl status; 3.登入数据库 Linux 设置 ...
- Python 中文文件统计词频 + 中文词云
1. 词频统计: import jieba txt = open("threekingdoms3.txt", "r", encoding='utf-8').re ...
- JAVA处理数字与中文数字互转(最大处理数字不超过万兆即:9999999999999999.9999)
package practice; import java.util.Arrays; /** * 数字与中文数字互转(最大处理数字不超过万兆即:9999999999999999.9999) * @au ...
- Image Processing and Analysis_15_Image Registration: A Method for Registration of 3-D shapes——1992
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- 【Day5】3.反爬策略之模拟登录
import urllib.request as ur import user_agent import lxml.etree as le request = ur.Request( url='htt ...
- Ubuntu系统---C++之VScode IDE 编译器安装
Ubuntu系统---C++之VScode IDE 编译器安装 简单了解了一下VScode,直观印象:安装包很小(不像VS那么大占用十G左右).跨平台.小巧.可以编译C++ / java / pyth ...
- sklearn逻辑回归实战
目录 题目要求 ex2data1.txt处理 方案一:无多项式特征 方案二:引入多项式特征 ex2data2.txt处理 两份数据 ex2data1.txt ex2data2.txt 题目要求 根据学 ...
- PhpStudy升级MySQL版本到5.7
1:备份当前数据库数据. 最好是导成 SQL 文件 2:备份 PhpStudy 下的 MySQL 文件夹.以防升级失败.还可以使用旧版本的数据库 3:下载MySQL5.7.解压.然后放在 PhpStu ...