ASP.NET MVC3 系列教程 – Web Pages 1.0

來源:互聯網
上載者:User
文章目錄
  • I:Web Pages 1.0中以“_”開頭的特別檔案(檔案命名時不區分大小寫)
  • II:關於多目錄下以”_”開頭的特殊檔案的執行順序
  • III:Web Pages 1.0脫離WebForms的啟動原理
  • IV:附錄:Global執行順序
  • V:有經驗的看官,或許已經知道了.可以在*.cshtml中做IoC(Unity 2.0有提供)來變Themes換布局等.

I:Web Pages 1.0中以“_”開頭的特別檔案(檔案命名時不區分大小寫)

“_appstart.cshtml” & “_pagestart.cshtml” & “_viewstart.cshtml”

_appstart.cshtml - 應用程式啟動時在Global. Application_Start方法後執行
功能:用於進行App的初始化時,需要進行處理的內容.例:向資料庫記錄系統初始化的一些資訊
功能與Global.Application_Start類似,差別在於:Global的Start先執行,然後在到該_appStart,值得注意的是在_appStart上下文中可以使用.NET4的dynamic新特性~~在聲明中,作為屬性、欄位、索引器、參數、傳回值或類型約束的類型。

http://msdn.microsoft.com/zh-cn/library/dd264741.aspx

code:

@{

    this.App.StartMessage = "App順利已啟動了.恭喜!哈";

    var error = this.App.Error as string;

    if (error == null)

    {

        this.App.Error = "使用dynamic新特性之前.請先賦值~";

        error = this.App.Error;

        @* 

            在這裡很遺憾地告訴大家.dynamic不支援智能感知 

            因為編譯無法100%準確得知程式的執行順序.

            所以無法智能感知!

        *@

    }

    // 在這裡可以引用 App.Error動態欄位了.

}

 
code:

//--------------------------------------------

@{

    @* ~/Views/_ViewStart.cshtml *@

    Response.Write(string.Format("<h1>{0}</h1>", App.StartMessage));

    Layout = "~/Views/Shared/_Layout.cshtml";

}

 
成員來自:

at System.Web.WebPages.Razor.WebPageRazorHost

at System.Web.WebPages.ApplicationStartPage

_viewstart.cshtml - 單個View處理Request時執行
功能:或許你已經聯想到了….Global的Page_Load(僅應用於View)……
執行順序位於_appstart.cshtml之後.畢竟所除層次不同
成員來自: 

at System.Web.Mvc.RazorViewEngine

綜上所述得知MVC3的APP初始化順序為:
 

(不排除本人未能發現的其他檔案類型,但目前據我所知道應用最廣的就這三個)

在Web Pages 1.0下,除非你顯式以”_”開頭命名View.否則你在請求”_”開頭的頁面時會遇到以下無法服務的頁面提示


(這圖在Razor文法基礎時就帖過了.這裡帖出來是讓大家溫故而知新)

關於*.cshtml產生的類名格式
絕大部分頁產生的程式集格式
 

頁面編譯都是以單獨頁面編譯為單個帶隨機字串的程式集,當然也可以採用先行編譯方式將n個頁編譯為1個程式集

II:關於多目錄下以”_”開頭的特殊檔案的執行順序

_appstart.cshtml僅能存在於根目錄(“~/”),
如果你在子目錄下放置_appstart.cshtml檔案的話.那麼該檔案就不會被App初始化時執行

當訪問~/somepage.cshtml時.
會先執行~/_pageStart.cshtml
然後在執行 ~/somepage.cshtml
當在複雜的子目錄環境下時:

~/_pageStart.cshtml

      ~/sub/_pageStart.cshtml

      ~/sub/somepage.cshtml

III:Web Pages 1.0脫離WebForms的啟動原理

首先Web Pages利用特性往本身程式集上與ASP.NET掛鈎
// SourceFile: AssemblyInfo.cs(System.Web.WebPages.dll)
//AttributeClass: System.Web. PreApplicationStartMethodAttribute
//特性介紹:為ASP.NET 其他Provide提供擴充
//參數1: ASP.NET Provide的類型
//參數2:啟動並執行方法名
//Source:
[assembly: PreApplicationStartMethod(typeof(System.Web.WebPages.PreApplicationStartCode), "Start")] //Line: 15
然後我們在這裡可以看到Web Pages的ASP.NET Provide是.Web.WebPages.PreApplicationStartCode
啟動方法是Start

public static void Start() {

    // Even though ASP.NET will only call each PreAppStart once, we sometimes internally call one 

    // another PreAppStart to ensure that things get initialized in the right order. ASP.NET does 

    // order so we have to guard against multiple calls.

    // All Start calls are made on same thread, so no lock needed here.

 

    if (_startWasCalled) {

        return;

    }

    _startWasCalled = true; //設定Start方法已被調用

 

    WebPageHttpHandler.RegisterExtension("cshtml");//註冊擴充

    WebPageHttpHandler.RegisterExtension("vbhtml");//註冊擴充

 

    // Turn off the string resource behavior which would not work in our simple base page

    PageParser.EnableLongStringsAsResources = false;//最佳化選項

 

    DynamicModuleUtility.RegisterModule(typeof(WebPageHttpModule));//重點在這裡了.~~註冊了一個WebPageHttpModule

 

    ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider();

    //ASP.NET Web Pages的RequestScopeStorageProvider

}

IV:附錄:Global執行順序

當WebApp開始運行時

Application_Start

Application_BeginRequest

Application_AuthenticateRequest

Session_Start

當WebApp終止運行時

Session_End

Application_End

當一個Request入站時

Application_BeginRequest

Application_AuthenticateRequest 過後到達*.cshtml

當在*.cshtml throw new Exception();時

Application_BeginRequest

Application_AuthenticateRequest

Application_Error(在throw處轉至,不會執行*.cshtml的throw後的下文)
例:
@{
Throw new Exception();//僅做樣本
//下文不會被執行,而直接跳到Application_Error終止Response
}

V:有經驗的看官,或許已經知道了.可以在*.cshtml中做IoC(Unity 2.0有提供)來變Themes換布局等.

更深入點,你可以dynamic建立*.cshtml檔案~~實現按需建立Web頁~HOHO~~~

@* MVC3一個新的Web開發革命正式誕生 *@

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.