Web Pages in ASP. net mvc 3 basic tutorial

Source: Internet
Author: User

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

 
 
  1.  
  2.  
  3. @{
  4. This. App. StartMessage = "the App has been successfully started. Congratulations! Ha ";
  5. Var error = this. App. Error as string;
  6. If (error = null)
  7. {
  8. This. App. Error = "before using the new dynamic feature, assign a value ~ ";
  9. Error = this. App. Error;
  10. @*
  11. It is a pity that dynamic does not support smart sensing.
  12. The program execution sequence cannot be accurately identified by 100% during compilation.
  13. Therefore, smart sensing is impossible!
  14. *@
  15. }
  16. // The App. Error dynamic field can be referenced here.
  17. }

 
 
  1. //--------------------------------------------  
  2. @{  
  3.     @* ~/Views/_ViewStart.cshtml *@  
  4.     Response.Write(string.Format("
  5.     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.

 
 
  1. // SourceFile: AssemblyInfo. cs (System. Web. WebPages. dll)
  2. // AttributeClass: System. Web. PreApplicationStartMethodAttribute
  3. // Feature Introduction: Provides extensions for other ASP. NET Provide
  4. // Parameter 1: ASP. NET Provide type
  5. // Parameter 2: name of the running Method
  6. // Source:
  7. [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.

 
 
  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 CILS.
  5. // All Start callare made on same thread, so no lock needed here.
  6. If (_ startWasCalled ){
  7. Return;
  8. }
  9. _ StartWasCalled = true; // The Start method has been called.
  10. WebPageHttpHandler. RegisterExtension ("cshtml"); // register the extension
  11. WebPageHttpHandler. RegisterExtension ("vbhtml"); // register the extension
  12. // Turn off the string resource behavior which wocould not work in our simple base page
  13. PageParser. EnableLongStringsAsResources = false; // Optimization Options
  14. DynamicModuleUtility. RegisterModule (typeof (WebPageHttpModule); // The focus is here .~~ Registered a WebPageHttpModule
  15. ScopeStorage. CurrentProvider = new AspNetRequestScopeStorageProvider ();
  16. // RequestScopeStorageProvider of ASP. NET Web Pages
  17. }

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 ();

 
 
  1. Application_BeginRequest
  2. Application_AuthenticateRequest
  3. Application_Error (go to at throw and do not execute the following after *. cshtml throw)
  4. Example:
  5. @{
  6. Throw new Exception (); // For example only
  7. // The following will not be executed, but will jump directly to Application_Error to terminate Response
  8. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.