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语法去直接解析你的页面从而把解析的页面生成静态页,这样的使用场景很多,不限于生成静态页面,视图引擎为我们提供了模型到视 ...
随机推荐
- element-ui重置表单并清除校验的方法
this.$refs['activityForm'].resetFields(); 只会重置之前表单的内容,并不会清空 只需在关闭弹框的cancel方法中写上重置表单的方法即可 cancel() { ...
- 解决ios中input兼容性问题
1.解决input输入框在iOS中有阴影问题 input{ -webkit-appearance: none; } 2.checkbox.raido在ios中阴影问题 单选: ...
- iOS 如何判断一个点在圆、方框、三角形区域内?
如何判断一个点是不是在方框(CGRect).圆(Circle).三角形(Triangle)内呢? 1.方框 //苹果官方方法可以判断 + (BOOL)point:(CGPoint)point inSq ...
- perl自定义简易的面向对象的栈与队列类
perl中的数组其实已经具备了栈与队列的特点,下面是对数组经过一些封装的stack,queue对象 1.Stack类 创建一个Stack.pm文件 package Stack; sub new{ $s ...
- Array + two points leetcode.18 - 4Sum
题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in num ...
- graph Laplacian 拉普拉斯矩阵
转自:https://www.kechuang.org/t/84022?page=0&highlight=859356,感谢分享! 在机器学习.多维信号处理等领域,凡涉及到图论的地方,相信小伙 ...
- 使用url_for()时,会自动调用转换器的to_url()方法
视图反推url,在动态url(转换器)反推中的应用 # -*- coding: utf-8 -*- from flask import Flask, url_for, redirect from we ...
- 《形式化分析工具Scyther性能研究》------摘抄整理
本篇论文的主要创新点在--------使用 Scyther工具发现对部分 KCI攻击搜索出现漏报的现象,并给出了存在的原因, 介绍了 形式化分析工具 AVispa全称是 Automated V ...
- Django中使用xadmin作为后台管理页面
xadmin后台管理 安装:luffy虚拟环境下 # >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 ...
- HttpClient获取数据
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 ...