详解ASP.NET Razor 语法

2025-05-29 0 49

razor 同时支持 c# (c sharp) 和 vb (visual basic)。

主要的 razor c# 语法规则

  • razor 代码块包含在 @{ … } 中
  • 内联表达式(变量和函数)以 @ 开头
  • 代码语句用分号结束
  • 变量使用 var 关键字声明
  • 字符串用引号括起来
  • c# 代码区分大小写
  • c# 文件的扩展名是 .cshtml

c# 实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<!-- single statement block -->

@{ var mymessage = "hello world"; }

<!-- inline expression or variable -->

<p>the value of mymessage is: @mymessage</p>

<!-- multi-statement block -->

@{

var greeting = "welcome to our site!";

var weekday = datetime.now.dayofweek;

var greetingmessage = greeting + " here in huston it is: " + weekday;

}

<p>the greeting is: @greetingmessage</p>

输出

the value of mymessage is: hello world

the greeting is: welcome to our site! here in huston it is: saturday

主要的 razor vb 语法规则

  • razor 代码块包含在 @code … end code 中
  • 内联表达式(变量和函数)以 @ 开头
  • 变量使用 dim 关键字声明
  • 字符串用引号括起来
  • vb 代码不区分大小写
  • vb 文件的扩展名是 .vbhtml

实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
<!-- single statement block -->

@code dim mymessage = "hello world" end code

<!-- inline expression or variable -->

<p>the value of mymessage is: @mymessage</p>

<!-- multi-statement block -->

@code

dim greeting = "welcome to our site!"

dim weekday = datetime.now.dayofweek

dim greetingmessage = greeting & " here in huston it is: " & weekday

end code

<p>the greeting is: @greetingmessage</p>

输出

the value of mymessage is: hello world

the greeting is: welcome to our site! here in huston it is: saturday

它是如何工作的?

razor 是一种将服务器代码嵌入在网页中的简单的编程语法

razor 语法是基于 asp.net 框架,专门用于创建 web 应用程序的部分 microsoft.net 框架。

razor 语法支持所有 asp.net 的功能,但是使用的是一种简化语法,对初学者而言更容易学习,对专家而言更有效率的。

razor 网页可以被描述成带以下两种类型内容的 html 网页: html 内容和 razor 代码。

当服务器读取页面时,它首先运行 razor 代码,然后再发送 html 页面到浏览器。在服务器上执行的代码能够执行一些在浏览器上不能完成的任务,比如,访问服务器数据库。服务器代码能创建动态的 html 内容,然后发送到浏览器。从浏览器上看,服务器代码生成的 html 与静态的 html 内容没有什么不同。

带 razor 语法的 asp.net 网页有特殊的文件扩展名 cshtml(razor c#)或者 vbhtml(razor vb)。

使用对象

服务器编码往往涉及到对象。

"date" 对象是一个典型的内置的 asp.net 对象,但对象也可以是自定义的,一个网页,一个文本框,一个文件,一个数据库记录,等等。

对象有用于执行的方法。一个数据库记录可能有一个 "save" 方法,一个图像对象可能有一个 "rotate" 方法,一个电子邮件对象可能有一个 "send" 方法,等等。
对象也有用于描述各自特点的属性。一个数据库记录可能有 firstname 和 lastname 属性。

asp.net date 对象有一个 now 属性(写成 date.now),now 属性有一个 day 属性(写成 date.now.day)。下面实例演示了如何访问 date 对象的一些属性:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
<table border="1">

<tr>

<th width="100px">name</th>

<td width="100px">value</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>

</td>

</table>

输出

name value
day 17
hour 21
minute 6
second 38

if 和 else条件

动态网页的一个重要特点是,您可以根据条件决定做什么。

做到这一点的常用方法是使用 if … else 语句:

?

1

2

3

4

5

6

7

8

9

10

11

12
@{

var txt = "";

if(datetime.now.hour > 12)

{txt = "good evening";}

else

{txt = "good morning";}

}

<html>

<body>

<p>the message is @txt</p>

</body>

</html>

输出

the message is good evening

读取用户输入

动态网页的另一个重要特点是,您可以读取用户输入。

输入是通过 request[] 功能读取的,并且传送输入数据是经过 ispost 条件判断的:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
@{

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 语法

以上就是详解asp.net razor 语法的详细内容,更多关于asp.net razor 语法的资料请关注快网idc其它相关文章!

原文链接:https://www.runoob.com/aspnet/razor-syntax.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解ASP.NET Razor 语法 https://www.kuaiidc.com/97715.html

相关文章

发表评论
暂无评论