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.

转载于:https://www.cnblogs.com/dereklovecc/p/3539940.html

Asp.net MVC razor语法参考相关推荐

  1. MVC Razor 语法(转)

    http://blog.sina.com.cn/s/blog_580e98300100yvz4.html 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Ra ...

  2. ASP.NET MVC Razor视图引擎

    本篇文章我们一起来讨论ASP.NET MVC框架中的Razor视图引擎.主要包含以下内容: Razor简介 Razor语法 Razor如何呈现页面 布局页(Layout) Razor简介 Razor是 ...

  3. asp.net mvc 包含了一个 html 的助手类在哪里,c# - 在App_Code中使用@HTML的ASP.NET MVC Razor Helper - 堆栈内存溢出...

    我尝试在cshtml文件中的App_Code中创建一个Helper. // Using's are needed to ensure helpers function correctly. @usin ...

  4. Asp.net MVC3 Razor语法小记

    1.在MVC View中使用 三元运算符的方式:@(bool?"":"") 2.在@{}中输出一行:@:<html> 3.Radio默认选中: @H ...

  5. mvc5控制器修改html,ASP.NET MVC Razor:如何在控制器动作中呈现Razor局部视图的HTML...

    我看到有人想知道如何为另一个控制器执行此操作. 就我而言,我的所有电子邮件模板都位于Views / Email文件夹中,但是您可以对其进行修改以传递与之关联了视图的控制器. public static ...

  6. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  7. 如何在 ASP.NET MVC 中集成 AngularJS

    介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...

  8. netcore 编译 html dll,ASP.NET Core Razor 视图预编译、动态编译

    0x01 前言 ASP.NET Core在默认发布情况下,会启动预编译将试图编译成xx.Views.dll,也许在视图中打算修改一处很细小的地方我们需要再重新编译视图进行发布.下面我将从 ASP.NE ...

  9. ASP.NET MVC 简单的分页思想与实现

    首先我们通过VS创建一个空的基于Razor视图引擎的ASP.NET MVC3 Web应用程序,命名为JohnConnor.Web 对创建过程或Razor不太了解的看官,请移步 ASP.NET MVC ...

最新文章

  1. 去除文本框点击的背影
  2. 教你从零开始使用wordpress做为后台生成小程序(小白版教程)
  3. spring jmx_JMX和Spring –第2部分
  4. pip加速+百度镜像|清华镜像
  5. Redis 缓存实战——缓存、数据库一致性问题分析与解决方案
  6. python 倒排索引(Inverted Index)
  7. 宠物火葬行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  8. 【java笔记】网络编程概述
  9. 你用java的swing可以做出这么炫的mp3播放器吗?
  10. 2021 年最佳开源软件榜单出炉
  11. 英特尔的新方向:称王集成计算设备领域
  12. 设计测试用例需要注意的点
  13. 搜集各种稀奇古怪的编码
  14. 辣子鸡-辣子鸡家常做法
  15. com.thoughtworks.xstream.converters.reflection.ReflectionConverter,用XSTream将xml转换为Java对象
  16. 一个通过SOAP web service驱动ssh/telnet执行命令的小平台
  17. django qq邮箱发送验证码
  18. 80核处理器_最受欢迎的处理器 酷睿i5-9400F果然霸榜了
  19. HDU 4508 湫湫系列故事――减肥记I
  20. MATLAB fprintf 函数输出希腊字母/特殊字符

热门文章

  1. 给大家推荐一款高逼格的Linux磁盘信息查看工具
  2. JAVA:说说你对序列化的理解
  3. Shell中I/O重定向的用法笔记
  4. 教你理清SpringBoot与SpringMVC的关系
  5. Web安全的三个攻防姿势
  6. 什么是 Native、Web App、Hybrid、React Native和Weex?
  7. 【收集】几款浏览器兼容性测试工具
  8. python3[进阶]8.对象引用、可变性和垃圾回收
  9. 安全态势感知产品对比_设计中的对比和人的感知
  10. 游戏 新手引导 设计_我认为每个新手设计师都应该知道什么