ASP.NET MVC 3: Razor视图引擎中 @: 和<text> 语法【转载】

(文章没翻译:建议大家读英文原文,看不懂查着看,顺便提高自己的英语水平!)

In today’s post I’m going to discuss two useful syntactical features of the new Razor view-engine – the @: and <text> syntax support.

Fluid Coding with Razor

ASP.NET MVC 3 ships with a new view-engine option called “Razor” (in addition to the existing .aspx view engine).  You can learn more about Razor, why we are introducing it, and the syntax it supports from my Introducing Razor blog post.

Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote the start and end of server blocks within your HTML. The Razor parser is smart enough to infer this from your code. This enables a compact and expressive syntax which is clean, fast and fun to type.

For example, the Razor snippet below can be used to iterate a list of products:

image

When run, it generates output like:

image

One of the techniques that Razor uses to implicitly identify when a code block ends is to look for tag/element content to denote the beginning of a content region.  For example, in the code snippet above Razor automatically treated the inner <li></li> block within our foreach loop as an HTML content block because it saw the opening <li> tag sequence and knew that it couldn’t be valid C#.

This particular technique – using tags to identify content blocks within code – is one of the key ingredients that makes Razor so clean and productive with scenarios involving HTML creation.

Using @: to explicitly indicate the start of content

Not all content container blocks start with a tag element tag, though, and there are scenarios where the Razor parser can’t implicitly detect a content block.

Razor addresses this by enabling you to explicitly indicate the beginning of a line of content by using the @: character sequence within a code block.  The @: sequence indicates that the line of content that follows should be treated as a content block:

image

As a more practical example, the below snippet demonstrates how we could output a “(Out of Stock!)” message next to our product name if the product is out of stock:

image

Because I am not wrapping the (Out of Stock!) message in an HTML tag element, Razor can’t implicitly determine that the content within the @if block is the start of a content block.  We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.

Using Code Nuggets within @: content blocks

In addition to outputting static content, you can also have code nuggets embedded within a content block that is initiated using a @: character sequence.

For example, we have two @: sequences in the code snippet below:

image

Notice how within the second @: sequence we are emitting the number of units left within the content block (e.g. - “(Only 3 left!”). We are doing this by embedding a @p.UnitsInStock code nugget within the line of content.

Multiple Lines of Content

Razor makes it easy to have multiple lines of content wrapped in an HTML element.  For example, below the inner content of our @if container is wrapped in an HTML <p> element – which will cause Razor to treat it as content:

image

For scenarios where the multiple lines of content are not wrapped by an outer HTML element, you can use multiple @: sequences:

image

Alternatively, Razor also allows you to use a <text> element to explicitly identify content:

image

The <text> tag is an element that is treated specially by Razor. It causes Razor to interpret the inner contents of the <text> block as content, and to not render the containing <text> tag element (meaning only the inner contents of the <text> element will be rendered – the tag itself will not).  This makes it convenient when you want to render multi-line content blocks that are not wrapped by an HTML element.

The <text> element can also optionally be used to denote single-lines of content, if you prefer it to the more concise @: sequence:

image

The above code will render the same output as the @: version we looked at earlier.  Razor will automatically omit the <text> wrapping element from the output and just render the content within it.

原文:http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

posted on 2011-01-18 16:36 搏击的小船 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/guanjie20/archive/2011/01/18/1938454.html

ASP.NET MVC 3: Razor视图引擎中 @: 和text 语法【转载】相关推荐

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

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

  2. 在razor视图引擎中转义@字符

    我正在使用Razor作为视图引擎创建一个示例ASP.NET MVC 3站点. 剃须刀语法以@字符开头,例如@RenderBody() . 如果我在cshtml页面上写@test,它会给我解析错误 CS ...

  3. ASP.NET MVC 最好的视图引擎是什么?

    在ASP.NET Core MVC应用程序中,视图引擎(view engine)负责处理发送给客户端的内容.MVC框架中默认的视图引擎称为Razor,用来为HTML文件添加注释说明并将这些动态内容插入 ...

  4. ASP.Net MVC开发基础学习笔记:三、Razor视图引擎、控制器与路由机制学习

    一.天降神器"剃须刀" - Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用W ...

  5. ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习

    一.天降神器"剃须刀" - Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用W ...

  6. [转]ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习

    [出处]http://www.cnblogs.com/edisonchou/p/3923475.html 关于机制的介绍,讲得不错,觉得可以参考着学习一下 1.1 千呼万唤始出来的MVC3.0 在MV ...

  7. Razor视图引擎、控制器与路由机制学习

    1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASPX引擎或者第三方的NVelocity ...

  8. ASP.NET MVC Razor视图引擎

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

  9. ASP.NET MVC的Razor引擎:IoC在View激活过程中的应用

    在<ASP.NET MVC的Razor引擎:RazorView>介绍BuildManagerCompiledView的时候,我们谈到默认使用的ViewPageActivator使用当前注册 ...

最新文章

  1. AI给老照片上色,真的准吗?技术圈和历史圈吵了1000帖
  2. 用WINRAR巧妙分割大压缩包
  3. Yii框架中使用PHPExcel导出Excel文件
  4. 22-win10下ElasticSearch.6.1.0与logstash6.1.0的整合
  5. 两个常用的Infopath Service读取域值的函数
  6. C#.NET验证码智能识别学习笔记---06 解决java jre问题:JTessBoxEditor.jar打开的时报找不到或无法加载主类 com.sun.tools.javac.Main错误
  7. clion 32位xp安装 64位软件
  8. win11退回win10一直黑屏转圈怎么办 win11退回win10黑屏转圈的解决方法
  9. Git HEAD detached from origin(冲突解决后无法正常push) 问题的解决方案
  10. Rainmeter音频频谱插件分享
  11. git gitgitgitgitgit
  12. html中用CSS设置背景
  13. java高校人员信息管理系统
  14. 编译,发版,链接库问题
  15. 华硕服务器怎么装win7系统教程视频,华硕电脑离线重装win7系统详细教程
  16. 数据挖掘技术特写(转)
  17. Win10 添加映射网络驱动器出现无法连接网络解决方案
  18. EasyMonitor 视频监控开发平台 安装教程(保姆式教程)
  19. mysql快速复制一张表_MySQL快速复制一张表
  20. 1分钟让别人喜欢你(三)

热门文章

  1. 实战渗透之一个破站日一天
  2. Linux下内存泄露工具
  3. cocos2d学习路线
  4. 《剑指offer》从尾到头打印链表
  5. 深入理解Spark 2.1 Core (十三):sparkEnv类源码分析
  6. 使用IntelliJ IDEA搭建多maven模块JAVA项目
  7. 工业界推荐系统实用分析技巧
  8. 推荐系统论文DSIN:Deep Session Interest Network
  9. Facebook力推导航库:React Navigation使用详解
  10. Vijos P1696 数与连分数【连分数】