Asp.net MVC razor语法参考
Razor语法的快捷参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/
只是copy下来便于查阅!
I gave a presentation to another team at Microsoft yesterday on ASP.NET MVC and the Razor view engine and someone asked if there was a reference for the Razor syntax.
It turns out, there is a pretty good guide about Razor available, but it’s focused on covering the basics of web programming using Razor and inline pages and not just the Razor syntax.
So I thought it might be handy to write up a a really concise quick reference about the Razor syntax.
| Syntax/Sample | Razor | Web Forms Equivalent (or remarks) |
|---|---|---|
| Code Block |
@{
|
<% |
| Expression (Html Encoded) |
<span>@model.Message</span> |
<span><%: model.Message %></span> |
| Expression (Unencoded) |
<span> |
<span><%= model.Message %></span> |
| Combining Text and markup |
@foreach(var item in items) {
|
<% foreach(var item in items) { %>
|
| Mixing code and Plain text |
@if (foo) {
|
<% if (foo) { %>
|
| Using block |
@ using (Html.BeginForm()) {
|
<% using (Html.BeginForm()) { %>
|
| Mixing code and plain text (alternate) |
@if (foo) {
|
Same as above |
| Email Addresses |
Hi philha@example.com |
Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter |
| Explicit Expression |
<span>ISBN@(isbnNumber)</span> |
In this case, we need to be explicit about the expression by using parentheses. |
| Escaping the @ sign |
<span>In Razor, you use the |
@@ renders a single @ in the response. |
| Server side Comment |
@* |
<%-- |
| Calling generic method |
@(MyClass.MyMethod<AType>()) |
Use parentheses to be explicit about what the expression is. |
| Creating a Razor Delegate |
@{
|
Generates a Func<T, HelperResult> that you can call from within Razor. See this blog post for more details. |
| Mixing expressions and text |
Hello @title. @name. |
Hello <%: title %>. <%: name %>. |
| NEW IN RAZOR v2.0/ASP.NET MVC 4 | ||
| Conditional attributes |
<div class="@className"></div> |
When className = null
<div></div> When <div class=""></div> When <div class="my-class"></div> |
| Conditional attributes with other literal values |
<div class="@className foo bar"> |
When className = null
<div class="foo bar"></div> Notice the leading space in front of <div class="my-class foo bar"> |
| Conditional data-* attributes.
data-* attributes are always rendered. |
<div data-x="@xpos"></div> |
When xpos = null or ""
<div data-x=""></div> When <div data-x="42"></div> |
| Boolean attributes |
<input type="checkbox" |
When isChecked = true
<input type="checkbox"
<input type="checkbox" /> |
| URL Resolution with tilde |
<script src="~/myscript.js"> |
When the app is at /
<script src="/myscript.js"> When running in a virtual application named <script src="/MyApp/myscript.js"> |
Notice in the “mixing expressions and text” example that Razor is smart enough to know that the ending period is a literal text punctuation and not meant to indicate that it’s trying to call a method or property of the expression.
Let me know if there are other examples you think should be placed in this guide. I hope you find this helpful.
UPDATE 12/30/2012:I’ve added a few new examples to the table of new additions to Razor v2/ASP.NET MVC 4 syntax. Razor got a lot better in that release!
Also, if you want to know more, consider buying the Progamming ASP.NET MVC 4 book. Full disclosure, I'm one of the authors, but the other three authors are way better.
Asp.net MVC razor语法参考的更多相关文章
- ASP.NET MVC Razor语法
ASP.NET MVC Razor语法 (一) 关于_ViewStart.cshtml文件 使用Razor模板引擎的话,会自动生成一个_ViewStart.cshtml文件.事实上,_View ...
- asp.net MVC Razor 语法(1)
Razor 不是编程语言.它是服务器端标记语言. 什么是 Razor ? Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时, ...
- ASP.NET MVC Razor语法及实例
1.混合HTML与Razor脚本 知识点:(1).cshtml怎样引用访问数据, (2).if for 与html嵌套 @using System.Data @using CIIC.TCP.Enti ...
- asp.net MVC Razor 语法(2)
变量是用于存储数据的命名实体. 变量 变量用于存储数据. 变量名必须以字母字符开头,不能包含空格和保留字符. 变量可以是某个具体的类型,指示其所存储的数据类型.字符串变量存储字符串值 ("W ...
- asp.net MVC Razor 语法(3)
编程逻辑:执行基于条件的代码. If 条件 C# 允许您执行基于条件的代码. 如需测试某个条件,您可以使用 if 语句.if 语句会基于您的测试来返回 true 或 false: if 语句启动代码块 ...
- ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理
ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的 ...
- MVC Razor 语法(转)
http://blog.csdn.net/pasic/article/details/7072340 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Razo ...
- 教你如何在 Javascript 文件里使用 .Net MVC Razor 语法
摘录 文章主要是介绍了通过一个第三方类库RazorJS,实现Javascript 文件里使用 .Net MVC Razor 语法,很巧妙,推荐给大家 相信大家都试过在一个 View 里嵌套使用 jav ...
- Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html
Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html 1.前言 上一篇文章我开源了轮子,Asp.net Core 3.1 Razor视图模版动态渲染PDF,然后,很 ...
随机推荐
- ScrollView 的使用(非原创)
1.ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳转来显示的视图.ScrollView只支持垂直滚动 <ScrollView xmlns:android=&quo ...
- UINavigationController 导航控制器
一.导航视图控制器 1.管理视图控制器 2.控制视图控制器之间的跳转 3.是以压栈和出栈的形式来管理视图控制器 4.导航视图控制器必须要设置根视图控制器 5.导航是视图控制器包含UINavigatio ...
- setInterval && setTimeout || 定时器
来自w3school的解释 定时器setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterva ...
- R语言学习笔记(数据的读取与保存)
library(MASS)#载入package MASSdata(package="MASS") #查看MASS中的数据集data(SP500,package="MASS ...
- 写jQuery插件时,一种更好的合并参数的方法
看到很多人写jQuery插件时居然这样合并参数: this.defaults = { 'color': 'red', 'fontSize': '12px', 'textDecoration':'non ...
- 关于url拼接传参数和利用view的字典传参数时,模板获取数据的方式问题
url = "{% url 'dashboard:internship-theme-stat' %}?teacher_name="+teacher_name+"& ...
- .net项目IIS、VS 附加进程调试
IIS调试 1.首先要把项目发布至IIS上,确保项目能正常运行. 2.从IIS上右键站点>管理网站>浏览 或者打开“内容视图“ 选择一个文件右键>浏览. 3.用vs打开该项目,选择 ...
- Uubntu scrot 的简单使用
scrot 是一个使用 imlib2 库截取屏幕和保存图像的的工具. 1:安装 #apt-get install scrot 2:查看帮助 #scrot -help -v, --version显示版本 ...
- LLinux系统编程(10)——进程间通信之管道
管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常说的管道多是指无名管道,无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别.有名管道叫named ...
- UESTC_邱老师选妹子(二) 2015 UESTC Training for Dynamic Programming<Problem I>
I - 邱老师选妹子(二) Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Su ...