Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Request Life Cycle - Application_BeginRequest

I have a image file in my sample Project. I am trying the URL as below.

http://localhost:49334/Chrysanthemum.jpg

I have a Application_BeginRequest event in my Global.asax file.

protected void Application_BeginRequest(Object sender, EventArgs e)
{
}

Query - This event is not getting fired when I request the above image by directly typing the URL above.


FROM MSDN - HttpApplication.BeginRequest Event - Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.

I want to make my all request to fire `Application_BeginRequest` Event
like image 971
Pankaj Avatar asked May 03 '12 03:05

Pankaj


1 Answers

The problem is probably because .jpg extension is not defaultly mapped to asp.net and is handled by IIS.

If you using IIS7 you can change this by set runAllManagedModulesForAllRequests to true.

<system.webServer>
 <modules runAllManagedModulesForAllRequests="true">
  ...
 </modules>
</system.webServer>

If still this event is not fired, you can try change global.asax like this

<%@ Application Language="C#" %>

<script runat="server">

    public override void Init()
    {
        this.BeginRequest += new EventHandler(global_asax_BeginRequest);        
        base.Init();
    }

    void global_asax_BeginRequest(object sender, EventArgs e)
    {

    }    

</script>

If you want handle only .jpg files, better way is to make HTTP handler and configure system.webServer > handlers and system.web > httpHandlers section in web.config to run this handler for .jpg requests.

like image 83
Jan Remunda Avatar answered Oct 19 '22 22:10

Jan Remunda