请记住上一章实例中的网页:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Web Pages Demo</title> </head> <body> <h1>Hello Web Pages</h1> </body> </html>现在向实例中添加一些 Razor 代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Web Pages Demo</title> </head> <body> <h1>Hello Web Pages</h1> <p>The time is @DateTime.Now</p> </body> </html>
该页面中包含普通的 HTML 标记,除此之外,还添加了一个 @ 标识的 Razor 代码。
Razor 代码能够在服务器上实时地完成多有的动作,并将结果显示出来。(您可以指定格式化选项,否则只会显示默认项。)
<!-- 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 + " Today is: " + weekDay; } <p>The greeting is: @greetingMessage</p>
<!-- 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 & " Today is: " & weekDay End Code <p>The greeting is: @greetingMessage</p>
如果您想学习更多关于 Razor、C#、Visual Basic 编程语言,请查看本教程的 Razor 部分。