自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET MVC 3基礎(chǔ)教程之Web Pages

開發(fā) 后端
ASP.NET MVC 3基礎(chǔ)教程之Web Pages 1.0,:用于進(jìn)行App的初始化時,需要進(jìn)行處理的內(nèi)容.例:向數(shù)據(jù)庫記錄系統(tǒng)初始化的一些信息。

image

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

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

_appstart.cshtml - 應(yīng)用程序啟動時在Global. Application_Start方法后執(zhí)行

功能:用于進(jìn)行App的初始化時,需要進(jìn)行處理的內(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

  1.  
  2.  
  3. @{  
  4.     this.App.StartMessage = "App順利已啟動了.恭喜!哈";  
  5.     var error = this.App.Error as string;  
  6.     if (error == null)  
  7.     {  
  8.         this.App.Error = "使用dynamic新特性之前.請先賦值~";  
  9.         error = this.App.Error;  
  10.         @*   
  11.             在這里很遺憾地告訴大家.dynamic不支持智能感知   
  12.             因為編譯無法100%準(zhǔn)確得知程序的執(zhí)行順序.  
  13.             所以無法智能感知!  
  14.         *@  
  15.     }  
  16.     // 在這里可以引用 App.Error動態(tài)字段了.  

image

  1. //--------------------------------------------  
  2. @{  
  3.     @* ~/Views/_ViewStart.cshtml *@  
  4.     Response.Write(string.Format("<h1>{0}</h1>", App.StartMessage));  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  

image 

成員來自:

at System.Web.WebPages.Razor.WebPageRazorHost

at System.Web.WebPages.ApplicationStartPage

_viewstart.cshtml - 單個View處理Request時執(zhí)行

功能:或許你已經(jīng)聯(lián)想到了….Global的Page_Load(僅應(yīng)用于View)……

執(zhí)行順序位于_appstart.cshtml之后.畢竟所除層次不同

成員來自:

at System.Web.Mvc.RazorViewEngine

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

image 

(不排除本人未能發(fā)現(xiàn)的其他文件類型,但目前據(jù)我所知道應(yīng)用最廣的就這三個)

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

image

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

關(guān)于*.cshtml生成的類名格式

絕大部分頁生成的程序集格式

image 

頁面編譯都是以單獨(dú)頁面編譯為單個帶隨機(jī)字符串的程序集,當(dāng)然也可以采用預(yù)編譯方式將n個頁編譯為1個程序集

II:關(guān)于多目錄下以”_”開頭的特殊文件的執(zhí)行順序

_appstart.cshtml僅能存在于根目錄(“~/”),

如果你在子目錄下放置_appstart.cshtml文件的話.那么該文件就不會被App初始化時執(zhí)行

當(dāng)訪問~/somepage.cshtml時.

會先執(zhí)行~/_pageStart.cshtml

然后在執(zhí)行 ~/somepage.cshtml

當(dāng)在復(fù)雜的子目錄環(huán)境下時:

~/_pageStart.cshtml

~/sub/_pageStart.cshtml

~/sub/somepage.cshtml

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

首先Web Pages利用特性往本身程序集上與ASP.NET掛鉤

  1. // SourceFile: AssemblyInfo.cs(System.Web.WebPages.dll)  
  2. //AttributeClass: System.Web. PreApplicationStartMethodAttribute  
  3. //特性介紹:為ASP.NET 其他Provide提供擴(kuò)展  
  4. //參數(shù)1: ASP.NET Provide的類型  
  5. //參數(shù)2:運(yùn)行的方法名  
  6. //Source:  
  7. [assembly: PreApplicationStartMethod(typeof(System.Web.WebPages.PreApplicationStartCode), "Start")] //Line: 15 

然后我們在這里可以看到Web Pages的ASP.NET Provide是.Web.WebPages.PreApplicationStartCode

啟動方法是Start

  1. public static void Start() {  
  2.     // Even though ASP.NET will only call each PreAppStart once, we sometimes internally call one   
  3.     // another PreAppStart to ensure that things get initialized in the right order. ASP.NET does   
  4.     // order so we have to guard against multiple calls.  
  5.     // All Start calls are made on same thread, so no lock needed here.  
  6.    
  7.     if (_startWasCalled) {  
  8.         return;  
  9.     }  
  10.     _startWasCalled = true//設(shè)置Start方法已被調(diào)用  
  11.    
  12.     WebPageHttpHandler.RegisterExtension("cshtml");//注冊擴(kuò)展  
  13.     WebPageHttpHandler.RegisterExtension("vbhtml");//注冊擴(kuò)展  
  14.    
  15.     // Turn off the string resource behavior which would not work in our simple base page  
  16.     PageParser.EnableLongStringsAsResources = false;//優(yōu)化選項  
  17.    
  18.     DynamicModuleUtility.RegisterModule(typeof(WebPageHttpModule));//重點(diǎn)在這里了.~~注冊了一個WebPageHttpModule  
  19.    
  20.     ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider();  
  21.     //ASP.NET Web Pages的RequestScopeStorageProvider  

IV:附錄:Global執(zhí)行順序

當(dāng)WebApp開始運(yùn)行時

Application_Start

Application_BeginRequest

Application_AuthenticateRequest

Session_Start

當(dāng)WebApp終止運(yùn)行時

Session_End

Application_End

當(dāng)一個Request入站時

Application_BeginRequest

Application_AuthenticateRequest 過后到達(dá)*.cshtml

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

  1. Application_BeginRequest  
  2. Application_AuthenticateRequest  
  3. Application_Error(在throw處轉(zhuǎn)至,不會執(zhí)行*.cshtml的throw后的下文)  
  4. 例:  
  5. @{  
  6. Throw new Exception();//僅做示例  
  7. //下文不會被執(zhí)行,而直接跳到Application_Error終止Response  

原文鏈接:http://www.cnblogs.com/highend/archive/2011/04/14/aspnet_mvc3_web_pages.html

【編輯推薦】

  1. 淺談ASP.NET MVC 3中如何使用Model
  2. 體驗ASP.NET MVC 3中的Razor特性
  3. MVC架構(gòu)模式為什么這樣“紅”?
  4. 專訪微軟MVP衣明志:走進(jìn)ASP.NET MVC 2框架開發(fā)
  5. 淺談ASP.NET MVC中TempData的實(shí)現(xiàn)機(jī)制
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2009-07-24 09:20:15

數(shù)組實(shí)例

2009-07-24 10:09:08

ASP.NET個性化ASP.NET基礎(chǔ)教程

2009-07-22 17:45:35

ASP.NET教程

2011-08-08 10:46:15

ASP.NET MVC

2009-08-01 20:59:08

ASP.NET服務(wù)器控ASP.NET服務(wù)器ASP.NET

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-07-24 11:25:53

Web應(yīng)用程序工程ASP.NET MVC

2009-07-30 14:18:02

ASP.NET實(shí)例教程

2009-07-23 12:22:41

ASP.NET MVC

2011-01-15 23:07:59

2010-10-12 09:52:02

ASP.NET MVC

2010-06-23 08:56:58

ASP.NET MVC

2017-07-18 10:14:23

OracleMerge into教程

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入門教程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號