Asp BeginRequest Event

Use of BeginRequest Event in Asp.Net

It is fired on every Request to the Application .

The order of Execution is as follows

The application events are raised in the following order:
BeginRequest
AuthenticateRequest
PostAuthenticateRequest
AuthorizeRequest
PostAuthorizeRequest
ResolveRequestCache
PostResolveRequestCache
PostMapRequestHandler
AcquireRequestState
PostAcquireRequestState
PreRequestHandlerExecute
The event handler is executed.
PostRequestHandlerExecute
ReleaseRequestState
PostReleaseRequestState
PostUpdateRequestCache
LogRequest.
PostLogRequest
EndRequest

This post Explains on how BeginReest can be used .
A page may be restricted based on some condition as folows.
To do this Implement a class that Inherits the IHttpModule and Implement the Init() and Dispose().
Handle the event BeginRequest As Folows

// In the Init()
context.BeginRequest += new EventHandler(context_BeginRequest);

void context_BeginRequest(object sender, EventArgs e)
{
// Called For every Request to the server
HttpRequest request = ((HttpApplication) sender).Request;
HttpContext context = ((HttpApplication)sender).Context;
string applicationPath = request.Path;

if(applicationPath == “/”)
{
applicationPath = “/Index.aspx”;
}
context.RewritePath(applicationPath);
Debug.WriteLine(“Begin Requst Fired”);

}

Here Whenever “/” is the requests path , the responce is redirected to the “/Index.aspx”.
It can be used for other such scenarios.

Hope It helped you 🙂 .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s