重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設、域名注冊、服務器等服務
為企業(yè)提供網(wǎng)站建設、域名注冊、服務器等服務
今天就跟大家聊聊有關如何理解ASP.NET MVC 中的Web Pages,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到臨縣網(wǎng)站設計與臨縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務覆蓋臨縣地區(qū)。
I:Web Pages 1.0中以“_”開頭的特別文件(文件命名時不區(qū)分大小寫)
“_appstart.cshtml” & “_pagestart.cshtml” & “_viewstart.cshtml”
_appstart.cshtml - 應用程序啟動時在Global. Application_Start方法后執(zhí)行
功能:用于進行App的初始化時,需要進行處理的內(nèi)容.例:向數(shù)據(jù)庫記錄系統(tǒng)初始化的一些信息
功能與Global.Application_Start類似,差別在于:Global的Start先執(zhí)行,然后在到該_appStart,值得注意的是在_appStart上下文中可以使用.NET4的dynamic新特性~~在聲明中,作為屬性、字段、索引器、參數(shù)、返回值或類型約束的類型。
http://msdn.microsoft.com/zh-cn/library/dd264741.aspx
@{ this.App.StartMessage = "App順利已啟動了.恭喜!哈"; var error = this.App.Error as string; if (error == null) { this.App.Error = "使用dynamic新特性之前.請先賦值~"; error = this.App.Error; @* 在這里很遺憾地告訴大家.dynamic不支持智能感知 因為編譯無法100%準確得知程序的執(zhí)行順序. 所以無法智能感知! *@ } // 在這里可以引用 App.Error動態(tài)字段了. }
//-------------------------------------------- @{ @* ~/Views/_ViewStart.cshtml *@ Response.Write(string.Format("{0}
", App.StartMessage)); Layout = "~/Views/Shared/_Layout.cshtml"; }
成員來自:
at System.Web.WebPages.Razor.WebPageRazorHost
at System.Web.WebPages.ApplicationStartPage
_viewstart.cshtml - 單個View處理Request時執(zhí)行
功能:或許你已經(jīng)聯(lián)想到了….Global的Page_Load(僅應用于View)……
執(zhí)行順序位于_appstart.cshtml之后.畢竟所除層次不同
成員來自:
at System.Web.Mvc.RazorViewEngine
綜上所述得知MVC3的APP初始化順序為:
(不排除本人未能發(fā)現(xiàn)的其他文件類型,但目前據(jù)我所知道應用最廣的就這三個)
在Web Pages 1.0下,除非你顯式以”_”開頭命名View.否則你在請求”_”開頭的頁面時會遇到以下無法服務的頁面提示
(這圖在Razor語法基礎時就帖過了.這里帖出來是讓大家溫故而知新)
關于*.cshtml生成的類名格式
絕大部分頁生成的程序集格式
頁面編譯都是以單獨頁面編譯為單個帶隨機字符串的程序集,當然也可以采用預編譯方式將n個頁編譯為1個程序集
II:關于多目錄下以”_”開頭的特殊文件的執(zhí)行順序
_appstart.cshtml僅能存在于根目錄(“~/”),
如果你在子目錄下放置_appstart.cshtml文件的話.那么該文件就不會被App初始化時執(zhí)行
當訪問~/somepage.cshtml時.
會先執(zhí)行~/_pageStart.cshtml
然后在執(zhí)行 ~/somepage.cshtml
當在復雜的子目錄環(huán)境下時:
~/_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提供擴展 //參數(shù)1: ASP.NET Provide的類型 //參數(shù)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方法已被調(diào)用 WebPageHttpHandler.RegisterExtension("cshtml");//注冊擴展 WebPageHttpHandler.RegisterExtension("vbhtml");//注冊擴展 // Turn off the string resource behavior which would not work in our simple base page PageParser.EnableLongStringsAsResources = false;//優(yōu)化選項 DynamicModuleUtility.RegisterModule(typeof(WebPageHttpModule));//重點在這里了.~~注冊了一個WebPageHttpModule ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider(); //ASP.NET Web Pages的RequestScopeStorageProvider }
IV:附錄:Global執(zhí)行順序
當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處轉(zhuǎn)至,不會執(zhí)行*.cshtml的throw后的下文) 例: @{ Throw new Exception();//僅做示例 //下文不會被執(zhí)行,而直接跳到Application_Error終止Response }
看完上述內(nèi)容,你們對如何理解ASP.NET MVC 中的Web Pages有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。