【问题标题】:ASP.Net Request Life Cycle - Application_BeginRequestASP.Net 请求生命周期 - Application_BeginRequest
【发布时间】:2012-05-03 03:53:58
【问题描述】:

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

http://localhost:49334/Chrysanthemum.jpg

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

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

查询 - 当我通过直接输入上面的 URL 来请求上面的图像时,这个事件不会被触发。


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

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

【问题讨论】:

  • 检查您是否确实在使用带有集成应用程序池的 IIS7。

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


【解决方案1】:

问题可能是因为 .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 处理程序并在 web 中配置 system.webServer > handlerssystem.web > httpHandlers 部分。配置为 .jpg 请求运行此处理程序。

【讨论】: