1. 什么是Razor
2. Razor C# 基本语法规则
3. 逻辑条件与循环
4. ASP.NET MVC 中 Razor 布局
@
将代码块添加到页面中<!-- Inline expressions -->
<p>You are using @Request.Broswer.Broswer!</p>
<!-- Single statement blocks -->
@{ ViewBag.title = "Home Page"; }
@{ var myMessage = "Hello World"; }
<!-- Multi-statement block -->
@{
var name = "Jason";
var greeting = "Nice to meet you, ";
var greetingMessage = greeting + name;
}
<p>The greeting is: @greetingMessage</p>
var
关键字,声明变量存储值<!-- Storing a string -->
@{ var welcomeMessage = "Welcome, new members!"; }
<p>@welcomeMessage</p>
<!-- Storing a date -->
@{ var year = DateTime.Now.Year; }
@{ var myString = "This is just an example"; }
@{ var test = "This is a long
string"; } // Does not work!
@helper formatAmount(decimal amount)
{
var color = "green";
if (amount < 0)
{
color = "red";
}
<span style="color:@color">@String.Format("{0:c}", amount)</span>
}
然后可以在其他地方使用helper方法,比如:
@{var amounts = new List<decimal> {100, 25.50m, -40, 276.99m}
}
<ul>
@foreach(decimal amount in amounts)
{
<li>@formatAmount(amount)</li>
}
</ul>
@{}
中的内容都会被视为C#代码如果想要添加纯文本,两种方法
@ {
//方法1
<text>djskfadsfhadsjfk</text>
//方法2
@: fhdshfjskhfksfs
}
输出@
符号
@ { <p>Have a good weekend @@LA</p> }
//output: Have a good weekend @LA
使用@**@
@* A one-line code comment. *@
@*
This is a multiline code comment.
It can continue for any number of lines.
*@
在@{}
中使用C#的注释格式
@{
// This is a comment.
var myVar = 17;
/* This is a multi-line comment
that uses C# commenting syntax. */
}
If-else, else if 语句
@ { var price = 25; }
<body>
@if (price >= 30)
{
<p>The price is high.</p>
}
else if (price > 20 && price < 30)
{
<p>The price is OK.</p>
}
else
{
<p>The price is low.</p>
}
</body>
Switch 语句
@ { var day = "Monday"; }
<body>
@switch(day)
{
case "Monday":
message="This is the first weekday.";
break;
case "Thursday":
message="Only one day before weekend.";
break;
case "Friday":
message="Tomorrow is weekend!";
break;
default:
message="Today is " + day;
break;
}
For 循环
<!-- 方式1 -->
@for (int i = 0; i < 10; i++)
{
@:@i
}
<!-- 方式2 -->
@{
for (int i = 0; i < 10; i++)
{
//do something
}
}
While 循环
<body>
@{
var i = 0;
while (i < 5)
{
i += 1;
<p>Output is: @i</p>
}
}
</body>
Foreach 循环
//定义一个数组
@{var amounts = new List<decimal> {100, 25.50m, -40, 276.99m}
}
//使用foreach遍历数组
<ul>
@foreach(decimal amount in amounts)
{
<li>@amount</li>
}
</ul>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
@Html.Partial("_header")
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
<div class="row">
<div class="col-md-12">
<img src="~/Content/Images/logo.png" class="img-responsive item-center"/>
</div>
</div>
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
@Html.Partial("_footer")
</body>
</html>
2024 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123