Marko Radosevic
Marko Radosevic

Reputation: 150

Event that runs on every request on C# web forms

I need event that runs before or on every request made on server.

If that event exist I need to run some code on that event.

For example: Someone types http://www.website.com/some-link

I need to check this link before on before it's processed by application.

I am using C# web forms.

So far I am trying to find event in Global.asax.cs, something like

protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
    // need to track requested url
    // code that needs to be run with requested url.
}

Is there any proper solution for this situation?

Tnx

Upvotes: 1

Views: 3841

Answers (1)

Thorarins
Thorarins

Reputation: 1904

the proper solution would be to use the event Application_BeginRequest, unless you want to complicate things and use httpmodules wich I wouldn't recommend.

protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
    var url = Request.Url.AbsoluteUri;
    var path = Request.Url.AbsolutePath;
    var host = Request.Url.Host;

    // your magic goes here
}

just as some information the order of all events is:

  • Application_BeginRequest
  • Application_AuthenticateRequest
  • Application_AuthorizeRequest
  • Application_ResolveRequestCache
  • Application_AcquireRequestState
  • Application_PreRequestHandlerExecute
  • Application_PreSendRequestHeaders
  • Application_PreSendRequestContent
    • asp.net page code is executed
  • Application_PostRequestHandlerExecute
  • Application_ReleaseRequestState
  • Application_UpdateRequestCache
  • Application_EndRequest

Upvotes: 12

Related Questions