Wednesday, March 08, 2006 10:23 PM
DmitryR
Child Requests in ASP.NET 2.0
The attached sample illustrates how to execute true server-side child requests that go though the entire HTTP pipeline in ASP.NET v2.0. The full source code is included.
Out of the box ASP.NET supports two similar features:
- Server.Execute. This method executes (calls ProcessRequest method on) the HTTP handler corresponding to the given path. None of the pipeline events (for HTTP Modules or GLOBAL.ASAX) are fired and thus, for example, authentication rules are by-passed.
- Response.Redirect. This method does re-enter the pipeline for the 'child request', but at a cost of the roundtrip to the client that issued the original request.
Child Request feature sample allows spawning of server-side requests that go through the entire HTTP pipeline and capturing of the responses. Several child requests could be used in the context of one parent request. In fact, no parent request is needed at all (which is yet another advantage over Server.Execute and Response.Redirect).
Child Request API
The API is quite simple to use:
- Create an instance of ChildRequest class
- Call ChildRequestExecutor.ExecuteChildRequest to execute the child request synchronously (BeginExecuteChildRequest and EndExecuteChildRequest for asynchronous child request execution)
- Obtain the response from ChildRequest properties (Status, Headers, Content).
In addition, the instance of the ChildRequest being executed can be retrieved from a module or a handler using ChildRequestExecutor.GetChildRequestFromContext. This can be useful in order to retrieve UserData property of ChildRequest set by the caller.
HTTP Pipeline in ASP.NET 2.0
Related to this feature, here is a quick reference for the steps in HTTP pipeline in ASP.NET 2.0. Some of the steps are internal (cannot be subscribed by HTTP modules or GLOBAL.ASAX):
- Internal step to validate request. Protects against malicious attacks exploiting path canonicalization
- Internal step to perform URL mapping (if the URL mapping feature is enabled)
- Fire BeginRequest event
- Fire AuthenticateRequest event
- Fire DefaultAuthentication internal event
- Fire PostAuthenticateRequest event
- Fire AuthorizeRequest event
- Fire PostAuthorizeRequest event
- Fire ResolveRequestCache event
- Fire PostResolveRequestCache event
- Internal step to determine the IHttpHandler to process the current request (this is when the page compilation takes place)
- Fire PostMapRequestHandler event
- Fire AcquireRequestState event
- Fire PostAcquireRequestState event
- Fire PreRequestHandlerExecute event
- Internal step to execute the IHttpHandler (call its ProcessRequest method) for the current request. The handler is determined at step #11
- Fire PostRequestHandlerExecute event
- Fire ReleaseRequestState event
- Fire PostReleaseRequestState event
- Internal step to perform response filtering (only if HttpResponse.Filter is installed)
- Fire UpdateRequestCache event
- Fire PostUpdateRequestCache event
- Fire EndRequest event. This is the only event that is guaranteed to be fired for each request