ASP.NET MVC 3基础教程之Web Pages

开发 后端
ASP.NET MVC 3基础教程之Web Pages 1.0,:用于进行App的初始化时,需要进行处理的内容.例:向数据库记录系统初始化的一些信息。

image

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

  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%准确得知程序的执行顺序.  
  13.             所以无法智能感知!  
  14.         *@  
  15.     }  
  16.     // 在这里可以引用 App.Error动态字段了.  

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时执行

功能:或许你已经联想到了….Global的Page_Load(仅应用于View)……

执行顺序位于_appstart.cshtml之后.毕竟所除层次不同

成员来自:

at System.Web.Mvc.RazorViewEngine

综上所述得知MVC3的APP初始化顺序为:

image 

(不排除本人未能发现的其他文件类型,但目前据我所知道应用最广的就这三个)

在Web Pages 1.0下,除非你显式以”_”开头命名View.否则你在请求”_”开头的页面时会遇到以下无法服务的页面提示

image

(这图在Razor语法基础时就帖过了.这里帖出来是让大家温故而知新)

关于*.cshtml生成的类名格式

绝大部分页生成的程序集格式

image 

页面编译都是以单独页面编译为单个带随机字符串的程序集,当然也可以采用预编译方式将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挂钩

  1. // SourceFile: AssemblyInfo.cs(System.Web.WebPages.dll)  
  2. //AttributeClass: System.Web. PreApplicationStartMethodAttribute  
  3. //特性介绍:为ASP.NET 其他Provide提供扩展  
  4. //参数1: ASP.NET Provide的类型  
  5. //参数2:运行的方法名  
  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//设置Start方法已被调用  
  11.    
  12.     WebPageHttpHandler.RegisterExtension("cshtml");//注册扩展  
  13.     WebPageHttpHandler.RegisterExtension("vbhtml");//注册扩展  
  14.    
  15.     // Turn off the string resource behavior which would not work in our simple base page  
  16.     PageParser.EnableLongStringsAsResources = false;//优化选项  
  17.    
  18.     DynamicModuleUtility.RegisterModule(typeof(WebPageHttpModule));//重点在这里了.~~注册了一个WebPageHttpModule  
  19.    
  20.     ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider();  
  21.     //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();时

  1. Application_BeginRequest  
  2. Application_AuthenticateRequest  
  3. Application_Error(在throw处转至,不会执行*.cshtml的throw后的下文)  
  4. 例:  
  5. @{  
  6. Throw new Exception();//仅做示例  
  7. //下文不会被执行,而直接跳到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架构模式为什么这样“红”?
  4. 专访微软MVP衣明志:走进ASP.NET MVC 2框架开发
  5. 浅谈ASP.NET MVC中TempData的实现机制
责任编辑:彭凡 来源: 博客园
相关推荐

2009-07-24 09:20:15

数组实例

2009-07-24 10:09:08

ASP.NET个性化ASP.NET基础教程

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服务器控ASP.NET服务器ASP.NET

2009-07-24 11:25:53

Web应用程序工程ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-07-30 14:18:02

ASP.NET实例教程

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 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

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

2011-04-14 09:19:22

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号