หน้าเว็บ ASP.NET - Objects
หน้าเว็บมักจะเกี่ยวกับออบเจ็กต์
วัตถุหน้า
คุณเคยเห็นวิธีการของ Page Object ที่ใช้งานอยู่แล้ว:
@RenderPage("header.cshtml")
@RenderBody()
ในบทที่แล้ว คุณเห็นคุณสมบัติ Page Object สองรายการถูกใช้ (IsPost และ Request):
If (IsPost) {
if (Request["Choice"] != null) {
เมธอดของออบเจ็กต์หน้าบางส่วน
Method | Description |
---|---|
href | Builds a URL using the specified parameters |
RenderBody() | Renders the portion of a content page that is not within a named section (In layout pages) |
RenderPage(page) | Renders the content of one page within another page |
RenderSection(section) | Renders the content of a named section (In layout pages) |
Write(object) | Writes the object as an HTML-encoded string |
WriteLiteral | Writes an object without HTML-encoding it first. |
คุณสมบัติอ็อบเจ็กต์หน้าบางส่วน
Property | Description |
---|---|
IsPost | Returns true if the HTTP data transfer method used by the client is a POST request |
Layout | Gets or sets the path of a layout page |
Page | Provides property-like access to data shared between pages and layout pages |
Request | Gets the HttpRequest object for the current HTTP request |
Server | Gets the HttpServerUtility object that provides web-page processing methods |
คุณสมบัติหน้า (ของวัตถุหน้า)
คุณสมบัติ Page ของ Page Object ให้การเข้าถึงข้อมูลที่แชร์ระหว่างหน้าและหน้าเค้าโครงเหมือนคุณสมบัติ
คุณสามารถใช้ (เพิ่ม) คุณสมบัติของคุณเองในคุณสมบัติหน้า:
- Page.Title
- Page.Version
- เพจ.อะไรก็ได้ที่คุณชอบ
คุณสมบัติของเพจมีประโยชน์มาก ตัวอย่างเช่น ทำให้สามารถตั้งชื่อเพจในไฟล์เนื้อหา และใช้ในไฟล์เลย์เอาต์ได้:
Home.cshtml
@{
Layout="~/Shared/Layout.cshtml";
Page.Title="Home Page"
}
<h1>Welcome to W3Schools</h1>
<h2>Web Site Main Ingredients</h2>
<p>A Home Page (Default.cshtml)</p>
<p>A Layout File (Layout.cshtml)</p>
<p>A Style Sheet (Site.css)</p>
Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<title>@Page.Title</title>
</head>
<body>
@RenderBody()
</body>
</html>