I: special files starting with "_" in Web Pages 1.0 (the file name is case-insensitive)
"_ Appstart. cshtml" & "_ pagestart. cshtml" & "_ viewstart. cshtml"
_ Appstart. cshtml-run the following command after the Global. Application_Start method when the application starts.
Function: content to be processed during App initialization. For example, some information initialized to the database is recorded.
Function and Global. application_Start is similar. The difference is that the Global Start is executed first and then to this _ appStart. It is worth noting that it can be used in the _ appStart context. new dynamic Features of NET4 ~~ The Declaration is a type of attribute, field, indexer, parameter, return value, or type constraint.
Http://msdn.microsoft.com/zh-cn/library/dd264741.aspx
-
-
- @{
- This. App. StartMessage = "the App has been successfully started. Congratulations! Ha ";
- Var error = this. App. Error as string;
- If (error = null)
- {
- This. App. Error = "before using the new dynamic feature, assign a value ~ ";
- Error = this. App. Error;
- @*
- It is a pity that dynamic does not support smart sensing.
- The program execution sequence cannot be accurately identified by 100% during compilation.
- Therefore, smart sensing is impossible!
- *@
- }
- // The App. Error dynamic field can be referenced here.
- }
- //--------------------------------------------
- @{
- @* ~/Views/_ViewStart.cshtml *@
- Response.Write(string.Format("
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
Members from:
At System. Web. WebPages. Razor. WebPageRazorHost
At System. Web. WebPages. ApplicationStartPage
_ Viewstart. cshtml-executed when a single View processes a Request
Function: Maybe you already think of it .... Global Page_Load (for View only )......
The execution sequence is after _ appstart. cshtml. After all, the layers are different.
Members from:
At System. Web. Mvc. RazorViewEngine
In conclusion, the APP initialization sequence of MVC3 is:
(Other file types that I cannot find are not excluded, but I know the three most widely used files)
In Web Pages 1.0, unless you explicitly name a View starting with "_", you will receive the following error message when requesting a page starting "_":
(This figure has been posted when Razor syntax is used. Here we post it to let everyone know about it)
About the format of class names generated by *. cshtml
Assembly format generated for most pages
Page compilation uses individual page compilation as a single assembly with a random string. Of course, you can also use the pre-Compilation Method to compile n pages into one assembly.
II: execution sequence of special files starting with "_" in multiple directories
_ Appstart. cshtml can only be stored in the root directory ("~ /"),
If you place the _ appstart. cshtml file in the subdirectory, the file will not be executed during App initialization.
When accessing ~ /Somepage. cshtml.
Will be executed first ~ /_ PageStart. cshtml
Then execute ~ /Somepage. cshtml
In a complex subdirectory environment:
~ /_ PageStart. cshtml
~ /Sub/_ pageStart. cshtml
~ /Sub/somepage. cshtml
III: How to enable Web Pages 1.0 from WebForms
First, Web Pages use features to hook up with ASP. NET on its own assembly.
- // SourceFile: AssemblyInfo. cs (System. Web. WebPages. dll)
- // AttributeClass: System. Web. PreApplicationStartMethodAttribute
- // Feature Introduction: Provides extensions for other ASP. NET Provide
- // Parameter 1: ASP. NET Provide type
- // Parameter 2: name of the running Method
- // Source:
- [Assembly: PreApplicationStartMethod (typeof (System. Web. WebPages. PreApplicationStartCode), "Start")] // Line: 15
Then, we can see that ASP. NET Provide of Web Pages is. Web. WebPages. PreApplicationStartCode.
The Start method is 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 CILS.
- // All Start callare made on same thread, so no lock needed here.
- If (_ startWasCalled ){
- Return;
- }
- _ StartWasCalled = true; // The Start method has been called.
- WebPageHttpHandler. RegisterExtension ("cshtml"); // register the extension
- WebPageHttpHandler. RegisterExtension ("vbhtml"); // register the extension
- // Turn off the string resource behavior which wocould not work in our simple base page
- PageParser. EnableLongStringsAsResources = false; // Optimization Options
- DynamicModuleUtility. RegisterModule (typeof (WebPageHttpModule); // The focus is here .~~ Registered a WebPageHttpModule
- ScopeStorage. CurrentProvider = new AspNetRequestScopeStorageProvider ();
- // RequestScopeStorageProvider of ASP. NET Web Pages
- }
IV: Appendix: Global execution sequence
When the WebApp starts running
Application_Start
Application_BeginRequest
Application_AuthenticateRequest
Session_Start
When the WebApp stops running
Session_End
Application_End
When a Request is inbound
Application_BeginRequest
Application_AuthenticateRequest and then reaches *. cshtml
When *. cshtml throw new Exception ();
- Application_BeginRequest
- Application_AuthenticateRequest
- Application_Error (go to at throw and do not execute the following after *. cshtml throw)
- Example:
- @{
- Throw new Exception (); // For example only
- // The following will not be executed, but will jump directly to Application_Error to terminate Response
- }