在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
I:Web Pages 1.0中以“_”开头的特别文件(文件命名时不区分大小写)“_appstart.cshtml” & “_pagestart.cshtml” & “_viewstart.cshtml” _appstart.cshtml - 应用程序启动时在Global. Application_Start方法后执行 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%准确得知程序的执行顺序. 所以无法智能感知! *@ } // 在这里可以引用 App.Error动态字段了. }
//-------------------------------------------- @{ @* ~/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时执行 at System.Web.Mvc.RazorViewEngine 在Web Pages 1.0下,除非你显式以”_”开头命名View.否则你在请求”_”开头的页面时会遇到以下无法服务的页面提示 关于*.cshtml生成的类名格式 II:关于多目录下以”_”开头的特殊文件的执行顺序_appstart.cshtml仅能存在于根目录(“~/”), ~/_pageStart.cshtml ~/sub/_pageStart.cshtml ~/sub/somepage.cshtml III:Web Pages 1.0脱离WebForms的启动原理首先Web Pages利用特性往本身程序集上与ASP.NET挂钩 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后的下文) V:有经验的看官,或许已经知道了.可以在*.cshtml中做IoC(Unity 2.0有提供)来变Themes换布局等.更深入点,你可以dynamic创建*.cshtml文件~~实现按需创建Web页~HOHO~~~ @* MVC3一个新的Web开发革命正式诞生 *@ |
请发表评论