http://www.w3schools.com/aspnet/razor_syntax.asp

Razor supports both C# (C sharp) and VB (Visual Basic).

Main Razor Syntax Rules for C#

  • Razor code blocks are enclosed in @{ ... }
  • Inline expressions (variables and functions) start with @
  • Code statements end with semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml

代码块被包括在@{...}

内置的表达式(变量和函数)以@开始

语句以分号结束

使用var进行变量的声明

字符串用引号

C#代码区分大小写

C#文件的扩展名为, .cshtml

How Does it Work?

Razor is a simple programming syntax for embedding server code in web pages.

Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.

The Razor syntax gives you all the power of ASP.NET, but is using a simplified syntax that's easier to learn if you're a beginner, and makes you more productive if you're an expert.

Razor web pages can be described as HTML pages with two kinds of content: HTML content and Razor code.

When the server reads the page, it runs the Razor code first, before it sends the HTML page to the browser. The code that is executed on the server can perform tasks that cannot be done in the browser, for example accessing a server database. Server code can create dynamic HTML content on the fly, before it is sent to the browser. Seen from the browser, the HTML generated by server code is no different than static HTML content.

ASP.NET web pages with Razor syntax have the special file extension cshtml (Razor using C#) or vbhtml (Razor using VB).


Working With Objects

Server coding often involves objects.

The "DateTime" object is a typical built-in ASP.NET object, but objects can also be self-defined, a web page, a text box, a file, a database record, etc.

Objects may have methods they can perform. A database record might have a "Save" method, an image object might have a "Rotate" method, an email object might have a "Send" method, and so on.

Objects also have properties that describe their characteristics. A database record might have a FirstName and a LastName property (amongst others).

The ASP.NET DateTime object has a Now property (written as DateTime.Now), and the Now property has a Day property (written as DateTime.Now.Day). The example below shows how to access some properties of the DateTime object:

<table border="1">
<tr>
<th width="100px">Name</th>
<td width="100px">Value</td>
</tr>
<tr>
<td>Year</td><td>@DateTime.Now.Year</td>
</tr>
<tr>
<td>Month</td><td>@DateTime.Now.Month</td>
</tr>
<tr>
<td>Day</td><td>@DateTime.Now.Day</td>
</tr>
<tr>
<td>Hour</td><td>@DateTime.Now.Hour</td>
</tr>
<tr>
<td>Minute</td><td>@DateTime.Now.Minute</td>
</tr>
<tr>
<td>Second</td><td>@DateTime.Now.Second</td>
</tr>
</table>

If and Else Conditions

An important feature of dynamic web pages is that you can determine what to do based on conditions.

The common way to do this is with the if ... else statements:

@{
var txt = "";
if(DateTime.Now.Hour > 12)
{txt = "Good Evening";}
else
{txt = "Good Morning";}
}
<html>
<body>
<p>The message is @txt</p>
</body>
</html>

Reading User Input

Another important feature of dynamic web pages is that you can read user input.

Input is read by the Request[] function, and posting (input) is tested by the IsPost condition:

@{
var totalMessage = "";
if(IsPost)
{
var num1 = Request["text1"];
var num2 = Request["text2"];
var total = num1.AsInt() + num2.AsInt();
totalMessage = "Total = " + total;
}
}
<html>
<body style="background-color: beige; font-family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1" /></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2" /></p>
<p><input type="submit" value=" Add " /></p>
</form>
<p>@totalMessage</p>
</body>
</html>

ASP.NET Razor - C# and VB Code Syntax的更多相关文章

  1. ASP.NET Razor C# 和 VB 代码语法

    ylbtech-.NET: ASP.NET Razor  C# 和 VB 代码语法 Razor 不是一种编程语言.它是服务器端的标记语言. 1. C# 和 VB 代码语法返回顶部 Razor 同时支持 ...

  2. ASP.NET Razor - 标记

    目录 什么是 Razor? Razor 帮助器 ASP.NET Razor - C# 和 VB 代码语法 主要的 Razor C# 语法规则 它是如何工作的? 使用对象 If 和 Else条件 读取用 ...

  3. ASP.NET Razor——Razor 简介

    ASP.NET Razor - 标记 Razor 不是一种编程语言.它是服务器端的标记语言. 什么是 Razor? Razor 是一种标记语法,可以让您将基于服务器的代码(Visual Basic 和 ...

  4. ASP.NET Razor 简介

    ylbtech-.NET: ASP.NET Razor 简介 Razor 不是一种编程语言.它是服务器端的标记语言. 1. 什么是 Razor?返回顶部 Razor 是一种标记语法,可以让您将基于服务 ...

  5. ASP.NET Razor 视图引擎编程参考

    ASP.NET Razor 视图引擎编程参考   转载请注明出处:http://surfsky.cnblogs.com Rasor 视图引擎    http://msdn.microsoft.com/ ...

  6. 分享自制的C#和VB Code互转工具

    分享自制的C#和VB Code互转工具 作为.NET程序员,往往习惯使用一种语言(据我观察,2006年后的程序员习惯用C#,之前的喜欢VB).而对于另一种语言虽然能读懂但是写起来总是比较费事.尤其面对 ...

  7. 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 ...

  8. ASP.NET Razor——ASP.NET Razor - C#代码语法

    Razor 同时支持 C# (C sharp) 和 VB (Visual Basic). 主要的 Razor C# 语法规则 Razor 代码块包含在 @{ ... } 中 内联表达式(变量和函数)以 ...

  9. ASP.NET Razor

    一.为什么要学习Razor? 可以让服务器代码(就是c#和vb)嵌入到网页中,也就是说这个页面中包含html代码和C#(vb)代码.基于服务器的代码可以在网页传送给浏览器时,创建动态 Web 内容.当 ...

随机推荐

  1. Vue跨域访问,axios&cors

    先安装node.js和npm,这个不用说了,直接在创建vue项目,然后实践一下跨域访问. 如果npm安装较慢,可安装淘宝镜像,执行下面命令: npm install -g cnpm --registr ...

  2. Android MediaRecorder自定义分辨率

    Android MediaRecorder自定义分辨率 工作这么久了,确实积累了不少东西,但都是以文档的形式存在U盘里的,为什么不写博客呢?因为懒啊!!!总感觉博客太难写了(大概是上学时候写作文恐惧症 ...

  3. kotlin第一个项目的搭建

    怎么在Android Studio中使用Kotlin? 1.使用Android Studio的插件 2.将Android Studio升级到3.0版本:目前不推荐,因为3.0的版本目前还是Dev Ch ...

  4. USB 接口探测分类

    USB 接口探测分类 SDP (Standand Downstream Port) 标准下行接口 标准USB都支持的接口 这种端口的D+和D-线上具有15kΩ下拉电阻.限流值如上讨论:挂起时为2.5m ...

  5. C# 检测字符串是否为数字

    long n; 1. ], ].All(char.IsDigit); //识别空字符时候 会认为是数字 string str0 = ""; string str1 = " ...

  6. WPF动态折线图

    此项目源码下载地址:https://github.com/lizhiqiang0204/WpfDynamicChart 效果图如下: 此项目把折线图制作成了一个控件,在主界面设置好参数直接调用即可,下 ...

  7. Super Poker II UVA - 12298 FFT_生成函数

    Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long #define double long do ...

  8. vue 导航菜单默认子路由

    export default new Router({ routes: [ { path: '/', name: 'index', component: index, children: [ { pa ...

  9. Clocksource tsc unstable

    内核在启动过程中会根据既定的优先级选择时钟源.优先级的排序根据时钟的精度与访问速度. 其中CPU中的TSC寄存器是精度最高(与CPU最高主频等同),访问速度最快(只需一条指令,一个时钟周期)的时钟源, ...

  10. [POJ2404]Jogging Trails

    我太弱了. 我们可以知道一个结论就是对于一个图的话假如所有点的度数都是偶数,那么只需要走一波欧拉回路. 所以我们就把奇点补成偶点. 将两个奇点补充到偶点的最佳方法是选择任意两个奇点连最短路径为权的边 ...