ASP. NET mvc3 series tutorial-web pages 1.0

Source: Internet
Author: User
ArticleDirectory
    • I: special files starting with "_" in Web pages 1.0 (the file name is case-insensitive)
    • II: execution sequence of special files starting with "_" in multiple directories
    • III: How to enable Web pages 1.0 from webforms
    • IV: Appendix: Global execution sequence
    • V: You may have an experienced viewer. You can use IOC in *. cshtml (provided by unity 2.0) to change themes layout.

I: special files starting with "_" in Web pages 1.0 (the file name is case-insensitive)

"_ Appstart. cshtml" & "_ pagestart. cshtml" & "_ viewstart. cshtml"

_ Appstart. cshtml-ApplicationProgramRun the following command after the global. application_start method.
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

Code:

 
@{

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.

 
}

 
Code:

 
//--------------------------------------------

 
@{

 
@*~ /Views/_ viewstart. cshtml *@

 
Response. Write (string. Format ("<H1>{0}</H1>", App. startmessage ));

 
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
}

V: You may have an experienced viewer. You can use IOC in *. cshtml (provided by unity 2.0) to change themes layout.

For more details, you can create a *. cshtml file in dynamic ~~ Create a web page as needed ~ Hoho ~~~

@ * Mvc3 a new Web Development Revolution officially emerged *@

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.