Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Web Pages in ASP.NET MVC

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to understand Web Pages in ASP.NET MVC. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Special files starting with "_" in I:Web Pages 1. 0 (files are not case-sensitive when named)

"_ appstart.cshtml" & "_ pagestart.cshtml" & "_ viewstart.cshtml"

_ appstart.cshtml-when the application starts in Global. Execute after the Application_Start method

Function: the content that needs to be processed when initializing App. Example: some information initialized to the database recording system

The function is similar to Global.Application_Start, except that the Start of Global is executed first, and then to the _ appStart, it is worth noting that the new dynamic feature of .NET 4 can be used in the context of _ appStart ~ ~ as a type of property, field, indexer, parameter, return value, or type constraint in the declaration.

Http://msdn.microsoft.com/zh-cn/library/dd264741.aspx

@ {this.App.StartMessage = "App has started smoothly. Congratulations! Ha "; var error = this.App.Error as string; if (error = = null) {this.App.Error =" before using the new dynamic feature. Please assign ~ "; error = this.App.Error; @ * it is a pity to tell you here that .dynamic does not support IntelliSense because the compiler cannot accurately know the execution order of the program. So you can't intelligently perceive! * @} / / you can reference the App.Error dynamic field here. }

/ /-@ {@ * ~ / Views/_ViewStart.cshtml * @ Response.Write (string.Format ("{0}", App.StartMessage)); Layout = "~ / Views/Shared/_Layout.cshtml";}

Members come from:

At System.Web.WebPages.Razor.WebPageRazorHost

At System.Web.WebPages.ApplicationStartPage

_ viewstart.cshtml-executed when a single View processes Request

Function: maybe you have thought of it. Page_Load of .Global (for View only).

The order of execution is after _ appstart.cshtml. After all, the levels are different.

Members come from:

At System.Web.Mvc.RazorViewEngine

To sum up, we know that the APP initialization order of MVC3 is as follows:

(I don't rule out other file types that I can't find, but as far as I know, these three are the most widely used.)

Under Web Pages 1. 0, unless you explicitly name View with "_". Otherwise, you will encounter the following unserviceable page prompts when you request a page that begins with "_".

This picture was posted on the basis of Razor grammar. This post is for everyone to review the past and learn the new.)

About the format of class names generated by * .cshtml

The assembly format generated by most pages

Page compilation is compiled as a single page into a single assembly with random strings, of course, n pages can be compiled into 1 assembly by precompilation.

II: about the execution order of special files starting with "_" in multiple directories

_ appstart.cshtml can only exist in the root directory ("~ /")

If you put the _ appstart.cshtml file in a subdirectory. Then the file will not be executed when initialized by App

When visiting ~ / somepage.cshtml.

Will execute ~ / _ pageStart.cshtml first

Then execute ~ / somepage.cshtml

When in a complex subdirectory environment:

~ / _ pageStart.cshtml

~ / sub/_pageStart.cshtml

~ / sub/somepage.cshtml

III:Web Pages 1.0 deviates from the startup principle of WebForms

First of all, Web Pages uses the feature to hook with ASP.NET on its own assembly

/ / SourceFile: AssemblyInfo.cs (System.Web.WebPages.dll) / / AttributeClass: System.Web. PreApplicationStartMethodAttribute / / feature introduction: provide extension / / parameter 1: ASP.NET Provide type / / parameter 2: running method name / / Source: [assembly: PreApplicationStartMethod (typeof (System.Web.WebPages.PreApplicationStartCode), "Start")] / / Line: 15 for other Provide of ASP.NET

Then we can see here that the ASP.NET Provide of WebPages is .Web.WebPages.PreApplicationStartCode

The startup 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 calls. / / All Start calls are made on same thread, so no lock needed here. If (_ startWasCalled) {return;} _ startWasCalled = true; / / the set Start method has been called WebPageHttpHandler.RegisterExtension ("cshtml"); / / registered extension WebPageHttpHandler.RegisterExtension ("vbhtml"); / / registered extension / / Turn off the string resource behavior which would not work in our simple base page PageParser.EnableLongStringsAsResources = false;// optimization option DynamicModuleUtility.RegisterModule (typeof (WebPageHttpModule)) / / that's the point. ~ ~ registered a RequestScopeStorageProvider of WebPageHttpModule ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider (); / / ASP.NET Web Pages}

IV: appendix: Global execution order

When WebApp starts running

Application_Start

Application_BeginRequest

Application_AuthenticateRequest

Session_Start

When WebApp stops running

Session_End

Application_End

When a Request enters the station

Application_BeginRequest

Arrive at * .cshtml after Application_AuthenticateRequest

When at * .cshtml throw new Exception ();

Application_BeginRequest Application_AuthenticateRequest Application_Error (after going to throw and will not execute * .cshtml throw) example: @ {Throw new Exception (); / only do examples / / the following will not be executed, but skip to Application_Error termination Response} after reading the above, do you have any further understanding of how to understand Web Pages in ASP.NET MVC? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report