Innards of asp.net
At the outset, before you read this article, I would like to make one thing clear. This article does not make any pretensions to being an original. I have referred to a number of links and I have tried to present in very simple words the inner workings of the asp.net engine. If you are interested in reading in more detail about how asp.net works internally, refer to the several links I have given at the end of this article.
Most of us work in asp.net at a very high level. We are familiar with the page life cycle; how different events are handled in event handlers like Textbox_Changed, Button_Click and so on. We know that the server controls are initialized in the Page_Init event handler and they are loaded in the Page_Load event handler. We are aware that the page is rendered in the Page_Render event handler. We are familiar with the class library which we use everyday and we are also familiar with the asp.net runtime.
But have you ever wondered how asp.net functions internally? If you are interested in getting some knowledge of the asp.net pipeline and how a request from a browser in the form of a URL, or a click on a link, or a HTTP form submitted through a post request is processed by asp.net, then read on. Firstly, as we all know the request is received by the web server which in the case of windows is IIS (Internet Information Services). Primarily asp.net is a request processing engine and the request is sent through the pipeline and we as developers attach code to process the request.
For understanding how this works we have to understand what is an ISAPI extension. I am not going to delve deeply into the details of ISAPI because it is a bit confusing and I want to keep this article as simple as possible. Just remember ISAPI is something that asp.net uses to interface with the web server or IIS. As you will see, this simple definition should suffice for our purposes. To those of you who are slightly more inquisitive I will add the definition that ISAPI is a low level unmanaged win32 style API.
I am going to deal with the one type of request that is very common in asp.net. The requests for web forms where the extension is .aspx is the most common among all requests. When IIS receives such a request the first thing ISAPI does is to examine the extension for the request. In the case of the .aspx extension, ISAPI directs the request to the appropriate HttpHandler or the aspnet_isapi.dll. Similarly ISAPI directs other requests received through IIS to the appropriate HttpHandler for that extension. Each handler is a .NET class that handles a specific type of request.
If you go to the IIS service manager you will be able to clearly see which request is mapped to which HttpHandler. This mapping is set by running an exe file called aspnet_regiis.exe. This cannot and should not be done manually. There are HttpHandler mappings for all ISAPI extensions. In addition it is also possible to create our own HttpHandlers and specify them in the web.config file. Next step is the entry of the request into the .NET runtime. The .NET runtime is hosted by two worker processes called ASPNET_WP.EXE (IIS5) and W3WP.EXE (IIS6). How the .NET runtime gets loaded when a request is sensed is not very well known.
The dll (aspnet_isapi.dll) to which the request has been forwarded from IIS by ISAPI, further forwards the request to the ISAPIRuntime class which is a .NET runtime class. This process is quiet complicated and I am not going into the details. This is the first entry point into the .NET runtime. To create the ISAPIRuntime class which receives the request, the System.Web.Hosting.AppDomainFactory.Create() method is called when the first request for a specific virtual directory is requested. So till here we can summarize as follows. IASPI forwards the request based on the extension to a HttpHandler which creates an entry point into the .NET runtime by instantiating the ISAPIRuntime class. This is the actual entry point into the asp.net pipeline.
Then the ISAPIRuntime.ProcessRequest() method of the IASPIRuntime class is invoked. This method then calls the HttpRuntime.ProcessRequest method which mainly mainly creates the HttpContext instance for the request and retrieves an HttpApplication instance. Each request is routed through a different HttpApplication instance. HttpApplication is responsible for moving the request through the asp.net pipeline. As the request moves through the pipeline a number of events fire and the event handler code gets executed.
Page_Init(), Page_load() and all other event handlers get executed as the events get fired when the request moves through the pipeline. The code you attach to each one of the postback event handlers like the Button_Click and Textbox_Changed etc also get executed as each one of the events get fired. Finally the page is served by IIS.
In short to give a rough overview of what happens I describe the steps below.
a) .aspx request sent
b) IIS receives request
c) ISAPI examines extension
d) Sends it to HttpHandler aspnet_isapi.dll
e) .NET runtime loaded
f) ISAPIRuntime class instantiated by calling System.Web.Hosting.AppDomainFactory.Create()
g) HttpContext instance for the request created and an HttpApplication instance retrieved
h) HttpApplication moves the request through asp.net pipeline
i) Events fire and the event handler code gets executed
j) Control returns to pipeline and page served by IIS
What I have described here is a very simplified version of what actually happens. If there are inaccuracies please do forgive me as I have depended on what I have read and understood from the net and tried to provide as simplified a version as possible without going into too much depth. If you are really interested in knowing things in detail refer to the links below.
A low-level Look at the ASP.NET Architecture
Where does ASP.NET Web API Fit?