Terminologies in MVC: Part 2 (Razor Engine Syntax vs Web Form)
Quick View
This article is all about the Razor Engine Syntax and traditional Web form explanations. I'll try to compare and differentiate between both using few parameters and some coding convention examples.
The following image says it all, but I'll try to explain all this in words and some code syntaxes.

So let's see what I have for you guys in this article. Here are some parameters based on which I'll compare both of the two.
Implicit Code Expression
Code expressions are generally evaluated and written to the response.
Code expressions in the Razor Engine are always HTML encoded but on the other hand in Web Forms the syntax automatically encodes the value.
In the following table I'll show how yu typically display a value in a view.
Let's have a look:
| Razor | <span>@model.Message</span> |
| Web Form | <span><%: model.Message %></span> |
Explicit Code Expression
In an explicit code expression, code expressions are evaluated and written to the response. In the following table I'll show how you typically would display a value in a view.
Let's have a look:
| Razor | <span>SSN@ (ssn)</span> |
| Web From | <span>SSN<%: ssn %></span> |
Encoded Code Expression
Let's have a close look at this definition:
"HTML encoded values are done by Encoded Code Expression."
| Razor | <span>@RazorViewEngine</span> |
| Web Form | <span><%: RazorEngineView %></span> |
Un-encoded Code Expression
Let's have a close look at this definition:
"Un-Encoded Code Expression is used for rendering un-encoded values."
In various cases we need to explicitly render some values that should not be HTML encoded. For that we need to use the Html.Raw method to ensure that the value is not encoded as determined. For this unencoded code expression with the Html.Raw method comes into the action.
Let's have a look at code expression:
| Razor | <span>@Html.Raw(model.Message)</span> |
| Web Form | <span><%: Html.Raw(model.Message)%></span> |
Code Block/Segment
Before going any further let's have a close look at the code block:
"Unlike code expressions, code blocks/segment are simply sections of code that are executed (evaluated and output to the response)."
- They are useful for declaring variable that you may need to use later.
- They are set of several code expressions.
- Evaluated one by one.
Let's have a look at code expression:
| Razor | @{ int A = 5; string C = "CSharpCorner."; } |
| Web Form | <% int A = 5; string C = "CSharpCorner."; %> |
Combining Text and Markup
This segment of article represents what an intermixing of text and markup looks like using both a Razor Engine and Web Form.
Let's have a look at code expression:
| Razor | @foreach (var item in items){ <span>Item @item.Name </span> } |
| Web Form | <% foreach (var item in items){ %> <span>Item <%: item.Name %></span> <% } %> |
Mixing Code and Plain Text
The Razor Engine always looks for the beginning of the tag to determine when to transition from code to markup but we often get some situations where we want to output the plain text immediately after a code block. Say, displaying some plain text within a conditional block.
Let's have a look at code expressions:
| Razor | @if (showMessage) { <h1>@ViewBag.Message</h1> } |
| Web Form | <% if (showMessage) { %> <h1><% : ViewBag.Message %></h1> <% } %> |
Server-side Comment
Let's have a close look at this definition:
"Server-side Comment syntax allows you to completely disable content/code/controls within a page."
- Helps in preventing the browser from parsing/running/displaying the HTML content within it.
- Server-side comments executes on the server.
- You can do server-side comments in both ways, either with the Razor Engine or with Web Forms.
| Razor | @* Its Multiline server-side comment. @if (showMessage) { <h1>@ViewBag.Message</h1> } This comment will be end here.. *@ |
| Web Form | <%-- Its Multiline server-side comment. <% if (showMessage) { %> <h1><% : ViewBag.Message %></h1> <% } %> This comment will be end here.. --%> |
Escaping the Code Delimiter
Delimiter
Before going any further let's have a look at some important keywords:
"A delimiter is a sequence of one or more characters used to specify the boundaries between separate, independent regions in plain text or other data streams."
An example of a delimiter is a "Comma"
- A delimiter specifies boundaries in the data stream, declaration notation.
- Delimiters can be broken down into Field-Record and Bracket Delimiter.
- Caused Delimiter Collision.
Escaping Delimiter
Now I'll explain what a delimiter is escaping.
Delimiter collision is a problem that occurs when a programmer introduces delimiters into code without actually intending them to be interpreted as boundaries between separate regions.
The following are methods that can be used for avoiding Delimiter collision:
- Escaping Character
- Escaping Sequence
- Dual Quoting Delimiter
- Padding Quoting Delimiter
- Multiple Quoting Delimiter
| Razor | My Twitter Handle is @jabhij (or you can write it as:) My Twitter Handle is @@jabhij |
| Web Form | <% Expression %> as you like it |
Calling a Generic Method
Generic method
Before going any further let's have a look at its definition:
"A Generic Method is a method that is declared with type parameters."
- A generic method can be overloaded on several type parameters.
- Within a generic class several non-generic methods can access several class level type parameters.
- Constraints can be used to enable more specialized operations on type parameters in methods.
| Razor | @(Html.MyMethod<AType>()) |
| Web Form | <%: Html.MyMethod<AType> () %> |
Points to Remember
The following are some important points that one needs to remember.
Razor Engine View
- Introduced with MVC 3.0
- Not a new language. It's a new "Markup Syntax"
- Supports Test Driven Dev (TDD)
- Has new advanced syntax that is compact, expressive and reduces typing
- Easy to learn and clean in compression to "Web Forms"
- Prevents Cross Site Scripting (XSS) attacks
- Has different file extensions.
- Doesn't support design mode.
Web Form Engine
- Introduced with MVC 1.0
- Doesn't support Test Driven Dev (TDD)
- System.Web.UI.Page Namespace is used in it for testing (but still it makes the testing complex)
- A bit faster in comparison to Razor
- Does not prevent from Cross Site Scripting (XSS) attacks
Summary
I hope this was helpful.
A few tips from my side is that, if you guys really want to become familiar with MVC then:
- Take your time
- Do as much practise as you can (Implementation)
- Try to "Learn" things, don't just "Study" (I mean it)
- Try to discuss things with friends, colleagues or in forums (C# Corner)
- If you experience any problem, then your queries are welcome.
I hope you guys enjoyed this.
Terminologies in MVC: Part 2 (Razor Engine Syntax vs Web Form)的更多相关文章
- 添加asp.net mvc到现有的asp.net web form 应用程序
前言 asp.net mvc的前一版本为asp.net web Form(Asp.net mvc之前称为asp.net),其第一个版本与2002年年初发布.asp.net web form 属于.ne ...
- ASP.NET MVC 3 and the @helper syntax within Razor
Friday, May 13, 2011 ASP.NET MVC 3 supports a new view-engine option called “Razor” (in addition to ...
- Razor Engine,实现代码生成器的又一件利器
Razor Engine,之前仅仅是ASP.NET MVC的一种View引擎,目前已经完全成为一种可以独立使用的模版引擎,并且已经成为了CodePlex上一个开源的项目(http://razoreng ...
- [2018-08-25]模板引擎Razor Engine 用法示例
好久没写博客了,回宁波后最近几个月一直忙些线下的事情. 敲代码方面脱产有阵子了,生疏了,回头一看,这行业果然更新飞快. 最近线下的事情基本忙完,准备开始干回老本行,最重要的一件事就是升级abplus库 ...
- 转发-基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现
基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现 概述 在日常开发中, 我们经常谈起模块化/插件化架构,这样可既可以提高开效率,又可以实现良好的扩展性,尤其对于产品化的系统有 ...
- (转)Asp.Net Mvc视图引擎Razor介绍
Asp.Net Mvc视图引擎Razor介绍 1.Razor介绍 程序园原创,转载请注明:http://www.kwstu.com/ArticleView/dabaomvc_2014082408205 ...
- asp.net mvc webform和razor的page基类区别
接触过asp.net mvc的都知道,在传统的webform的模式下,page页面的基类是这样声明的: <%@ Page Language="C#" MasterPageFi ...
- Web Form 和asp.net mvc 差别
Asp.net MVC 和web Form的基本区别 Web Form ASP.NET MVC 视图和逻辑紧密耦合 视图和逻辑分离 页面(给予文件的URL) 控制器(基于路由的URL) 状态管理(视图 ...
- web form中自定义HttpHandler仿mvc
web form中自定义HttpHandler仿mvc 前言 在mvc大行其道的今天,仍然有不少公司的项目还是使用web form来实现的(其实mvc也是基于web form的),如果要在项目中引入m ...
随机推荐
- 自动工作负载库理论与操作(Automatic Workload Repository,AWR)
AWR的由来: 10g之前的oracle:用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接 断开后,其原来 ...
- List<Map> 排序
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.u ...
- DHCP(四)
确认阶段:即DHCP服务器确认分配级DHCP客户端IP地址的阶段.某个DHCP服务器在收到DHCP客户端发来的DHCP Request报文后,只有DHCP客户端选择的服务器会进行如下操作:如果确认将地 ...
- 蓝桥杯 算法训练 ALGO-114 黑白无常
算法训练 黑白无常 时间限制:1.0s 内存限制:256.0MB 问题描述 某寝室的同学们在学术完之后准备玩一个游戏:游戏是这样的,每个人头上都被贴了一张白色或者黑色的纸,现在每个人都会说一句 ...
- RHEL6 64位ASM方式安装oracle 11gR2(二)
本文转载自:http://vnimos.blog.51cto.com/2014866/1221377 三.安装数据库软件 1 2 3 4 5 6 7 8 # unzip -d /stage/ linu ...
- git 本地文件里内容 操作记录
本地环境文件合并分支(以下的都分别 commit提交了的) [一.分支[追加] 和 [新增] 新信息 合并主线 情景] 分支内容: dr.find_element_by_id("su&qu ...
- 移植OpenWrt到CuHead Pro WiFi
移植OpenWrt到CuHead Pro WiFi Posted by: zou, baozhu , 三月 13, 2014 CuHead Pro是一款路由器开发板,下面是开发板的配置信息. 名称 型 ...
- 动态代理AOP实现方法过滤
上一节实现了动态代理,接下来 有时候,我不需要在每一个方法都要记录日志,做权限验证 等等. 所有就有了这样的需求.AOP实现特定方法过滤,有选择性的来对方法实现AOP 拦截.就是本节标题所示. 举个例 ...
- 判断修改的中的值,用前面的,否则容易获得空值;this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (bool)this.dataGridView1.Rows[i].Cells[0].Value;
判断修改的中的值,用前面的,否则容易获得空值:this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (b ...
- ubuntu14.10下Qt5.4无法输入中文
最近学习Qt,于是在ubuntu下安装了开发环境.我是从官网上下载安装的Qt5.4版本.安装后发现在开发过程中无法输入中文.于是在网上搜了搜.解决办法如下: 1.安装fcitx-frontend-qt ...