Web Pages razor 學習

來源:互聯網
上載者:User

標籤:

1. Web Pages razor

Web Pages 是三種 ASP.NET 編程模型中的一種,用於建立 ASP.NET 網站和 web 應用程式。

其他兩種編程模型是 Web Forms 和 MVC (Model, View, Controller)。

Web Pages 是最簡單的 ASP.NET 網頁開發編程模型。它提供了一種簡單的方法將 HTML、CSS、JavaScript 以及伺服器代碼結合起來:

  • 易於學習、閱讀和使用
  • 圍繞單一網頁進行構建
  • 類似 PHP 和 ASP
  • 伺服器指令碼使用 Visual Basic 或 C#
  • 對 HTML、CSS、JavaScript 的完全控制

Web Pages 通過可程式化的 Web Helpers 進行擴充,包括資料庫、視頻、映像、社交網路等等。

2. 學習實踐

   1. 開發環境vs2010 ,sql server 2012 

   2. 專案檔布局:

     

        幾個名稱的解釋:

Web 啟動之前:_AppStart

大多數伺服器端代碼都是在獨立的網頁中編寫的。例如,假如網頁包含輸入表單,那麼該網頁通常包含用於讀取資料的伺服器代碼。

不過,通過在網站根目錄中建立名為 _AppStart 的頁面,您能夠在網站啟動前就已執行啟動代碼(startup code)。假如該頁面存在,

ASP.NET 會在網站內的任何頁面被請求之前首先運行這個頁面。

通常 _AppStart 用於啟動代碼和全域值的初始化,比如計數器和全域名稱。

注釋 1:_AppStart 的副檔名應與網頁相同,比如 _AppStart.cshtml。

注釋 2:_AppStart 有底線首碼。正因如此,使用者才無法直接查看該檔案。

每張頁面之前:_PageStart

就像在網站啟動前運行 _AppStart 那樣,您能夠在每個檔案夾中的任意頁面之前運行代碼。

對於 web 中的每個檔案夾,您能夠添加名為 _PageStart 的檔案。

通常 _PageStart 用於設定檔案夾中所有頁面的布局檔案,或者在運行頁面之前檢查使用者登入。

流程

當請求到來時,ASP.NET 檢查 _AppStart 是否存在。如果存在,並且這是對網站的第一次請求,則運行 _AppStart。

然後 ASP.NET 檢查 _PageStart 是否存在。如果存在,則運行 _PageStart,在被請求的頁面之前。

如果您在 _PageStart 內部引用了對 RunPage() 的調用,那麼您可以規定在何處運行被請求的頁面。否則,_PageStart 在被請求頁面之前運行。

代碼部分解釋:

    Login.cshtml

@{
// LayoutPage = "layout.cshtml";
this.Layout = "layout.cshtml";  使用模板布局 
@:dalong
}
@{

var database = Database.Open("database");
var data = database.Query("select top 100 PSNName, CorpName from myperson2 ");   查詢資料庫
var grid = new WebGrid(data);  資料繫結顯示
foreach (var item in data)
{
<li>the age is :@item.PSNName
</li>
<li>the name is : @item.CorpName
</li>
}
}
@section dalong33 {    節點布局測試
<div id="grid">
@grid.GetHtml()
</div>
}
@{

var username = string.Empty;

username = Request["username"];   請求參數的擷取
if (username != null)
{
<form action="login" method="post">
<label for="username">
使用者名稱</label><input type=text name="username" [email protected] /><br>
<label for="pass">
密 碼</label><input type="text" name="pass" /><br>
</form>
}
}

     layout.cshtml

<! doctype html>
<html>

<head>

</head>

<body>

@RenderBody()

@RenderSection("dalong33",false);   節點布局(false 節點不是必須的)
</body>


</html>

_AppStart.cshtml    應用啟動執行的

@{

Context.Response.Write("<script>alert(‘dalong start app‘)</script>");

}

 _PageStart.cshtml  每個頁面執行的

 

@{

//Output.Write();

Context.Response.Write("<script>alert(‘app‘)</script>");
}

測試的介面比較簡單,不用貼圖了。

注意對於所需的dll 的引用

  • Microsoft.Web.Infrastructure.dll
  • NuGet.Core.dll
  • System.Web.Helpers.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.Administration.dll
  • System.Web.WebPages.Deployment.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • WebMatrix.Data.dll
  • WebMatrix.WebData  

Web Pages razor 學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.