ASP.Net请求生命周期 - Application_BeginRequest

时间:2012-05-03 03:53:58

标签: c# asp.net c#-4.0 iis-7

我的示例项目中有一个图像文件。我正在尝试以下网址。

  

http://localhost:49334/Chrysanthemum.jpg

我的Application_BeginRequest event文件中有一个Global.asax

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

查询 - 当我通过直接输入上述网址请求上述图片时,不会触发此事件。


FROM MSDN - HttpApplication.BeginRequest Event - 当ASP.NET响应请求时,作为HTTP管道执行链中的第一个事件发生。

I want to make my all request to fire `Application_BeginRequest` Event

1 个答案:

答案 0 :(得分:5)

问题可能是因为.jpg扩展名没有默认映射到asp.net,而是由IIS处理。

如果使用IIS7,可以通过将runAllManagedModulesForAllRequests设置为true来更改此值。

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

如果仍未触发此事件,您可以尝试像这样更改global.asax

<%@ 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>

如果您只想处理.jpg文件,更好的方法是制作HTTP处理程序并配置 system.webServer&gt;处理程序 system.web&gt; web.config中的httpHandlers 部分为.jpg请求运行此处理程序。