What happens when default.aspx page is requested开发者_开发技巧 for the first time?
.net is pure a pure Object Oriented Framework.
_default is a class which extends Page.
Without instantiating, Pre-Init,Init,Load cannot be called. So How _default class is instantiated? Who is responsible for that?
I want to know very detailed technical steps?
Pleasa clarify!
The ASP.Net framework identifies that the request is for the page default.aspx
and examines the markup of the corresponding .aspx file - using this it generates a class based on that markup. The base class for that class is identified in the @Page
directive:
<%@ Page ... Inherits="WebApplication1._Default" %>
It then creates an instance of that generated type - this type inherits from the given base class, in this case WebApplication1._Default
.
The ASP.Net framework doesn't normally (ever?) directly create an instance of your "code behind" class.
You can see this for yourself by debugging a simple web application:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// default_asp NOT _Default as you might expect
string typeName = this.GetType().Name;
}
}
This explains why event handlers only need to be marked as protected instead of public.
If you are really keen you can get the path to the generated assembly using this.GetType().Assembly.CodeBase
, make a copy of that file and inspect the generated class in something like IL Spy.
I suggest you read the ASP.NET Page Life Cycle Overview
The ASP.NET Page Object Model
When the request is for an .aspx resource, the handler is a page handler—namely, an instance of a class that inherits from Page. The association between types of resources and types of handlers is stored in the configuration file of the application
The type of the HTTP handler for a particular page depends on the URL. The first time the URL is invoked, a new class is composed and dynamically compiled to an assembly. The source code of the class is the outcome of a parsing process that examines the .aspx sources. The class is defined as part of the namespace ASP and is given a name that mimics the original URL. For example, if the URL endpoint is page.aspx, the name of the class is ASP.Page_aspx. The class name, though, can be programmatically controlled by setting the ClassName attribute in the @Page directive.
The base class for the HTTP handler is Page. This class defines the minimum set of methods and properties shared by all page handlers. The Page class implements the IHttpHandler interface.
Under a couple of circumstances, the base class for the actual handler is not Page but a different class. This happens, for example, if code-behind is used. Code-behind is a development technique that insulates the code necessary to a page into a separate C# or Microsoft Visual Basic® .NET class. The code of a page is the set of event handlers and helper methods that actually create the behavior of the page. This code can be defined inline using the tag or placed in an external class—the code-behind class. A code-behind class is a class that inherits from Page and specializes it with extra methods. When specified, the code-behind class is used as the base class for the HTTP handler.
精彩评论