How IIS handles incoming ASP.NET requests

Description
Table of Contents
Summary

Description of how ASP.NET requests are processed by the IIS.

Description

Overview

The processment of a request is based on the extension of the file on the web request.

When a request arrives at an IIS Web server, the IIS examines the requested file’s extension to determine how to handle the request.

After identifiying the extension, the request can be:

- natively handled by IIS (e.g. HTML pages, images and static content); OR
- routed through mapping to an ISAPI extension.

The mapping extension/handler DLL can be manually configured on the IIS.

Example:

To request an ASP.NET file, a client can request a URL like http://SERVER_NAME/aspnet_isapi.dll/pagefilename.aspx because ASP.NET files are processed by the ISAPI extension named %windir%\system32\inetsrv\aspnet_isapi.dll.
However, to simplify ASP.NET requests, IIS uses a script mapping that associates “.aspx” file name extensions with aspnet_isapi.dll.

What is an ISAPI extension ?

An ISAPI extension is an unmanaged, compiled Win32 DLL that handles the incoming request and generates the content
for a requested resource, based on the file type, in the response sent and rendered on the Client browser.

When the ISAPI extension is executed IIS raises several events during the lifetime of handling the request.
Those events can be handled by ISAPI filters.

What is an ISAPI filter ?

An ISAPI filter is an unmanaged, compiled Win32 DLL that contain code to respond to the events raised by the IIS.

The ASP.NET Resource

By default .aspx, .asp, .vb, .cs, .asmx, ashx, .svc extensions are mapped to asp_isapi.dll and aspnet_isapi.dll.

When these ISAPI are executed, the request is processed inside the ASP.NET engine worker process and it:

  1. Raises events, similarly to ISAPI filters, that can be handled by ASP.NET modules components;
  2. Delegates rendering the request resource, similarly to ISAPI extensions, to ASP.NET handlers components. ASP.NET includes several built-in handlers, e.g:
  • PageHandlerFactory used to render ASP.NET pages
  • WebServiceHandlerFactory used to render the response SOAP for ASP.NET web services

The ASP.NET worker process:

  1. Initializes all the configured modules: Each configured module is invoked for the the correspondent raised event;
  2. Determines the handler to be executed;
  3. Returns the generated response back to the requesting client


Events raised on the ASP.NET pipeline


Event Name Description
AcquireRequestState This event is raised when ASP.NET runtime is ready to acquire the Session state of the current HTTP request.
AuthenticateRequest This event is raised when ASP.NET runtime is ready to authenticate the identity of the user.
AuthorizeRequest This event is raised when ASP.NET runtime is ready to authorize the user for the resources user is trying to access.
BeginRequest This event is raised when ASP.NET runtime receives a new HTTP request.
Disposed This event is raised when ASP.NET completes the processing of HTTP request.
EndRequest This event is raised just before sending the response content to the client.
Error This event is raised when an unhandled exception occurs during the processing of HTTP request.
PostRequestHandlerExecute This event is raised just after HTTP handler finishes execution.
PreRequestHandlerExecute This event is raised just before ASP.NET begins executing a handler for the HTTP request. After this event, ASP.NET will forward the request to the appropriate HTTP handler.
PreSendRequestContent This event is raised just before ASP.NET sends the response contents to the client. This event allows us to change the contents before it gets delivered to the client. We can use this event to add the contents, which are common in all pages, to the page output. For example, a common menu, header or footer.
PreSendRequestHeaders This event is raised just before ASP.NET sends the HTTP response headers to the client. This event allows us to change the headers before they get delivered to the client. We can use this event to add cookies and custom data into headers.
ReleaseRequestState This event is raised after ASP.NET finishes executing all request handlers.
ResolveRequestCache This event is raised to determine whether the request can be fulfilled by returning the contents from the Output Cache. This depends on how the Output Caching has been setup for your web application.
UpdateRequestCache This event is raised when ASP.NET has completed processing the current HTTP request and the output contents are ready to be added to the Output Cache. This depends on how the Output Caching has been setup for your Web application.

Four other specific events can be directly handled on the global.asax file:

Event Name Description
Application_OnStart This event is raised when the very first request arrives to the Web application:
This event gets triggered only once during the life cycle of the application. This once happens when the first request for any resource in the application comes. Resource can be a page or an image in the application. When the very first request for a resource, say a web page, is made by a user “Application_Start” is triggered after which this event is not at all executed. If by any chance the server where the application is hosted is restarted then this event is fired once again i.e. when the very first request for any resource in the application is made after the server is reset
Application_OnEnd This event is raised just before the application is going to terminate:
This event is executed only once, when the application is unloaded. This event is the end event of “Application_Start” and is normally fired when the application is taken offline or when the server is stopped.
Session_OnStart This event is raised for the very first request of the user’s session.
This event is fired only when a new session for a user starts. Once “Session_Start” for a user is fired then if the user makes subsequent request to any resource within the application this event is not at all triggered. The event is triggered only when the user’s session expires and then the user tries to access any resource in the application again. The event doesn’t get triggered if you sessions are disabled in the web.config
Session_OnEnd This event is raised when the session is abandoned or expired:
This is the closing event of “Session_Start” event. Whenever a user’s session in the application expires this event gets fired. The session expiration time can be set in web.config file. By default session time out is set to 20 mins.

Operations
How to map a request extension to ISAPI extension applications on IIS

In the dialog box “Application Configuration” (IIS6.0) it’s possible to customize the mapping between file extensions and ISAPI extensions.

The “extension” can be populated with a simple extension wildcard or path:
- *.jpg
- RESTservices/*.jpg

How to do it

How to handle events raised by IIS within the ASP.NET engine pipeline

An ASP.NET application can have multiple custom modules implemented. For all the requests each of the configured modules is initialized and allowed to process each raised event during the processment of the request.

The article “HTTP Handler Description” on references section describes how to handle one or more ASP.NET events.

How to delegate requests processment within the ASP.NET engine worker process

The handler to be executed is determined after initializing all the configured modules.

The article “HTTP Module Description” on references section describes how to implement custom process to handle a specific type of request

References

[1] URL Rewriting in ASP.NET
[2] IIS URL Rewriting and ASP.NET Routing
[3] HTTP Handlers and HTTP Modules in ASP.NET
[4] 
HTTP Handlers Description
[5] HTTP Module Description