123456<html>
<body>
<h1>Hello Web Pages</h1>
<p>The time is @DateTime.Now</p>
</body>
</html>
@{...}
包围@
开头var
关键字声明变量.cshtml
12345678910111213<!-- 单行代码块 -->
@{ var myMessage = "Hello World"; }
<!-- 行内表达式或变量 -->
<p>The value of myMessage is: @myMessage</p>
<!-- 多行代码块 -->
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Today is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>
经过Web Pages,能够使用@RenderPage()
从不一样文件导入。html
内容块可以输入到网页中任意位置,可包含文本、标记和代码web
12345678<html>
<body>
@RenderPage("header.cshtml")
<h1>Hello Web Pages</h1>
<p>This is a paragraph</p>
@RenderPage("footer.cshtml")
</body>
</html>
布局页相似于普通网页。但在引用内容页的位置调用。数据库
每一个内容页面必须以Layout开头。服务器
123456789@{Layout="Layout.cshtml";}
<h1>Welcome to W3Schools</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laborisnisi ut aliquip ex ea commodo consequat.
</p>
在ASP.NET中,名称如下划线开头的文件没法经过 web 来浏览。布局
若是您但愿禁止用户查看内容块或布局文件,请对文件从新命名:_header.cshtml
_footer.cshtml
_Layout.cshtml
ui
在ASP.NET中,隐藏敏感信息(数据库密码、电邮密码等)的经常使用方法是把这些信息保存在名为 “_AppStart” 的独立文件中。spa
1234567@{
WebMail.SmtpServer = "mailserver.example.com";
WebMail.EnableSsl = true;
WebMail.UserName = "username@example.com";
WebMail.Password = "your-password";
WebMail.From = "your-name-here@example.com";
}