语法/例子 | Razor | Web Forms 写法 |
---|---|---|
代码块 | | |
表达式(Html 编码) | | |
表达式(未编码) | | |
Combining Text and markup | | |
Mixing code and Plain text | | |
Mixing code and plain text (alternate) | | Same as above |
Email Addresses | | Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter |
Explicit Expression | | In this case, we need to be explicit about the expression by using parentheses. |
Escaping the @ sign | | @@ renders a single @ in the response. |
Server side Comment | | |
Calling generic method | | 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. Seethis blog post for more details. |
Mixing expressions and text | | |
NEW IN RAZOR v2.0/ASP.NET MVC 4 | ||
Conditional attributes | | When When className = "" When className = "my-class" |
Conditional attributes with other literal values | | When Notice the leading space in front of foo is removed.When className = "my-class" |
Conditional data-* attributes. data-* attributes are always rendered. | | When When xpos = "42" |
Boolean attributes | | When isChecked = true When isChecked = false |
URL Resolution with tilde | | When the app is at / When running in a virtual application named MyApp |
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.
原始帖子:
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
其他好文章:
1)ASP.NET MVC 3: Razor’s @: and <text> syntax:
http://weblogs.asp.net/scottgu/archive/2010/12/16/asp-net-mvc-3-implicit-and-explicit-code-nuggets-with-razor.aspx
2)ASP.NET MVC 3: Razor’s @: and <text> syntax
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx