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
@{
int x = 123;
string y = "because.";
}
<%
int x = 123;
string y = "because.";
%>
Expression (Html Encoded)
<span>@model.Message</span>
<span><%: model.Message %></span>
Expression (Unencoded)
<span>
@Html.Raw(model.Message)
</span>
<span><%= model.Message %></span>
Combining Text and markup
@foreach(var item in items) {
<span>@item.Prop</span>
}
<% foreach(var item in items) { %>
<span><%: item.Prop %></span>
<% } %>
Mixing code and Plain text
@if (foo) {
<text>Plain Text</text>
}
<% if (foo) { %>
Plain Text
<% } %>
Using block
@ using (Html.BeginForm()) {
<input type="text" value="input here">
}
<% using (Html.BeginForm()) { %>
<input type="text" value="input here">
<% } %>
Mixing code and plain text (alternate)
@if (foo) {
@:Plain Text is @bar
}
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
@@foo to display the value
of foo</span>
@@ renders a single @ in the response.
Server side Comment
@*
This is a server side
multiline comment
*@
<%--
This is a server side
multiline comment
--%>
Calling generic method
@(MyClass.MyMethod<AType>())
Use parentheses to be explicit about what the expression is.
Creating a Razor Delegate
@{
Func<dynamic, object> b =
@<strong>@item</strong>;
}
@b("Bold this")
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 className = ""

<div class=""></div>

When className = "my-class"

<div class="my-class"></div>
Conditional attributes with other literal values
<div class="@className foo bar">
</div>
When className = null

<div class="foo bar"></div>

Notice the leading space in front of foo is removed. 
When className = "my-class"

<div class="my-class foo bar">
</div>
Conditional data-* attributes.

data-* attributes are always rendered.

<div data-x="@xpos"></div>
When xpos = null or ""

<div data-x=""></div>

When xpos = "42"

<div data-x="42"></div>
Boolean attributes
<input type="checkbox"
checked="@isChecked" />
When isChecked = true

<input type="checkbox"
checked="checked" />

When isChecked = false

<input type="checkbox" />
URL Resolution with tilde
<script src="~/myscript.js">
</script>
When the app is at /

<script src="/myscript.js">
</script>

When running in a virtual application named MyApp

<script src="/MyApp/myscript.js">
</script>

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语法参考的更多相关文章

  1. ASP.NET MVC Razor语法

    ASP.NET MVC Razor语法 (一) 关于_ViewStart.cshtml文件     使用Razor模板引擎的话,会自动生成一个_ViewStart.cshtml文件.事实上,_View ...

  2. asp.net MVC Razor 语法(1)

    Razor 不是编程语言.它是服务器端标记语言. 什么是 Razor ? Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时, ...

  3. ASP.NET MVC Razor语法及实例

    1.混合HTML与Razor脚本 知识点:(1).cshtml怎样引用访问数据, (2).if  for 与html嵌套 @using System.Data @using CIIC.TCP.Enti ...

  4. asp.net MVC Razor 语法(2)

    变量是用于存储数据的命名实体. 变量 变量用于存储数据. 变量名必须以字母字符开头,不能包含空格和保留字符. 变量可以是某个具体的类型,指示其所存储的数据类型.字符串变量存储字符串值 ("W ...

  5. asp.net MVC Razor 语法(3)

    编程逻辑:执行基于条件的代码. If 条件 C# 允许您执行基于条件的代码. 如需测试某个条件,您可以使用 if 语句.if 语句会基于您的测试来返回 true 或 false: if 语句启动代码块 ...

  6. ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理

    ASP.NET没有魔法——ASP.NET MVC Razor与View渲染   对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的 ...

  7. MVC Razor 语法(转)

    http://blog.csdn.net/pasic/article/details/7072340 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Razo ...

  8. 教你如何在 Javascript 文件里使用 .Net MVC Razor 语法

    摘录 文章主要是介绍了通过一个第三方类库RazorJS,实现Javascript 文件里使用 .Net MVC Razor 语法,很巧妙,推荐给大家 相信大家都试过在一个 View 里嵌套使用 jav ...

  9. Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html

    Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html 1.前言 上一篇文章我开源了轮子,Asp.net Core 3.1 Razor视图模版动态渲染PDF,然后,很 ...

随机推荐

  1. <modules runAllManagedModulesForAllRequests="true" />(转1)

    最近在使用 MVC 开发的时候,遇到一个对我来说“奇怪的问题”,就是使用 BundleTable 进行 CSS.JS 文件绑定,然后使用 Styles.Render.Scripts.Render 进行 ...

  2. iOS 之 cocoapods安装与使用

    我们都知道第三方库,一般使用cocoapods管理,cocoapods在我们IOS开发中有着很大的作用. 好了,现在看下它的安装步骤: 1.打开终端,输入 sudo gem install cocoa ...

  3. boost vc编译

    0.下载:http://www.boost.org/ 1.编译b2.exe,bjam.exe. 双击根目录下面的bootstrap.bat文件,生成b2.exe,bjam.exe(或者使用vs的命令行 ...

  4. python调用ice接口

    今天用python调用ice接口,遇到如下提示 ImportError: No module named Ice 解决方案是 set PYTHONPATH=C:\Program Files\ZeroC ...

  5. 极客”一词,来自于美国俚语“geek”的音译,一般理解为性格古怪的人

    起源 “ 极客”一词,来自于美国俚语“ geek”的音译,一般理解为性格古怪的人.数学“极客”大多是指,并不 一定是数学专业但又对数学等技术有狂热的兴趣并投入大量时间钻研的人.又 译作“ 奇客”.以前 ...

  6. Jquery网页选项卡应用

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. 微信开发之门店管理{"errcode":40097,"errmsg":"invalid args hint: [xxxxxxx]"}

    最近在做微信端开发,做到门店开发部分,在创建门店的时候遇到40097问题{"errcode":40097,"errmsg":"invalid args ...

  8. zabbix之1监控概念

    1.通过通用的snmp监控,无需代理端 2.通过snmp代理 snmp代理的工作原理:在被监控端设置代理,代理不断的获取本地数据,而管理端定期通过代理获取监控数据. snmp目前有v1,v2,v3三种 ...

  9. H5本地存储

    在HTML5中可以把数据长期存储在客户端,使用的对象就是localStorage. localStorage常用方法有setItem.getItem.removeItem.clear. 下面是一个存储 ...

  10. wifi智能插座 一键扫描局域网内插座Ip及其它信息 Python源代码API

    转载请保留原地址. http://www.cnblogs.com/lsqandzy 最近买了几个智能插座玩,插座安装在家里,连接好wifi,不管你人在哪里,通过手机,一键开启或关闭插座电源,想象一下, ...