【发布时间】:2016-12-17 00:36:49
【问题描述】:
从运行在 IIS 7.5 服务器上的ASP.NET / C# Web 应用程序下载文件时,如何截取响应中正在下载的 pdf 文件?响应具有Content-Type 的application/pdf 和Content-Length 的10091。
目前我写了一个覆盖方法的HttpModule:
public void Init(HttpApplication context){
context.BeginRequest+=Context_BeginRequest();
context.EndRequest+=Context_EndRequest();
}
private void Begin_Request(Object sender, EventArgs e){
// this function does not get called when downloading a file!
}
private void End_Request(Object sender, EventArgs e){
// this function does not get called when downloading a file!
}
为了拦截文件下载响应,我需要重写哪个函数?
web.config 看起来像:
<system.webServer>
<modules runallManagedModulesForAllRequests="true">
<add name="MyMod.Module" type="MyMod.Module"/>
</modules>
</system.webServer>
【问题讨论】:
-
模块注册了吗?
-
是的,该模块已注册,因为这两个函数都是在正常 Web 流量(即查看网页)上调用的。但是当我尝试下载文件时,两个函数都没有被调用。
-
我不是这个级别的专家,但可能对那种文件类型或所有静态文件的请求不会通过 aspnet 管道。
-
我添加了我的 web.config 的外观。没有像@Jason 提到的那样的先决条件。
-
尝试通过将 *.pdf 映射到您的处理程序或模块来将其添加到 IIS 管理器(而不是 web.config)中。