简体   繁体   English

第一次请求default.aspx页面时会发生什么?

[英]What happens when default.aspx page is requested for the first time?

What happens when default.aspx page is requested for the first time? 第一次请求default.aspx页面时会发生什么?

.net is pure a pure Object Oriented Framework. .net是纯粹的面向对象的框架。

_default is a class which extends Page. _default是一个扩展Page的类。

Without instantiating, Pre-Init,Init,Load cannot be called. 无需实例化,无法调用Pre-Init,Init,Load。 So How _default class is instantiated? 那么_default类是如何实例化的呢? Who is responsible for that? 谁对此负责?

I want to know very detailed technical steps? 我想知道非常详细的技术步骤?

Pleasa clarify! Pleasa澄清!

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. ASP.Net框架确定请求是针对页面default.aspx并检查相应的.aspx文件的标记 - 使用它来生成基于该标记的类。 The base class for that class is identified in the @Page directive: 该类的基类在@Page指令中标识:

<%@ 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 . 然后它创建该生成类型的实例 - 此类型继承自给定的基类,在本例中为WebApplication1._Default

The ASP.Net framework doesn't normally (ever?) directly create an instance of your "code behind" class. ASP.Net框架通常(永远不会)直接创建“代码隐藏”类的实例。

You can see this for yourself by debugging a simple web application: 您可以通过调试简单的Web应用程序来自行查看:

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 . 如果您真的很热衷,可以使用this.GetType().Assembly.CodeBase获取生成的程序集的路径,制作该文件的副本并检查生成的类,如IL Spy

The ASP.NET Page Object Model ASP.NET页面对象模型

When the request is for an .aspx resource, the handler is a page handler—namely, an instance of a class that inherits from Page. 当请求是针对.aspx资源时,处理程序是页面处理程序 - 即继承自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. 特定页面的HTTP处理程序的类型取决于URL。 The first time the URL is invoked, a new class is composed and dynamically compiled to an assembly. 第一次调用URL时,将组成一个新类并将其动态编译为程序集。 The source code of the class is the outcome of a parsing process that examines the .aspx sources. 该类的源代码是检查.aspx源的解析过程的结果。 The class is defined as part of the namespace ASP and is given a name that mimics the original URL. 该类被定义为命名空间ASP的一部分,并被赋予一个模仿原始URL的名称。 For example, if the URL endpoint is page.aspx, the name of the class is ASP.Page_aspx. 例如,如果URL端点是page.aspx,则该类的名称为ASP.Page_aspx。 The class name, though, can be programmatically controlled by setting the ClassName attribute in the @Page directive. 但是,可以通过在@Page指令中设置ClassName属性来以编程方式控制类名。

The base class for the HTTP handler is Page . HTTP处理程序的基类是Page This class defines the minimum set of methods and properties shared by all page handlers. 此类定义所有页面处理程序共享的最小方法和属性集。 The Page class implements the IHttpHandler interface. Page类实现了IHttpHandler接口。

Under a couple of circumstances, the base class for the actual handler is not Page but a different class. 在某些情况下,实际处理程序的基类不是Page而是另一个类。 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. 代码隐藏是一种开发技术,它将页面所需的代码隔离到单独的C#或MicrosoftVisualBasic®.NET类中。 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. 代码隐藏类是一个继承自Page的类,并使用额外的方法对其进行专门化。 When specified, the code-behind class is used as the base class for the HTTP handler. 指定时,代码隐藏类用作HTTP处理程序的基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM