【问题标题】:Intercept a file download in C# using either an http module or http handler?使用 http 模块或 http 处理程序在 C# 中拦截文件下载?
【发布时间】:2016-12-17 00:36:49
【问题描述】:

从运行在 IIS 7.5 服务器上的ASP.NET / C# Web 应用程序下载文件时,如何截取响应中正在下载的 pdf 文件?响应具有Content-Typeapplication/pdfContent-Length10091

目前我写了一个覆盖方法的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)中。

标签: c# asp.net intercept


【解决方案1】:

你没有在 web.config 中显示你是如何注册模块的,但是如果它看起来像这样:

<system.webServer>
  <modules>
    <add name="mymodule" type="typename, assemblyname" preCondition="managedHandler" />
  </modules>
</system.webServer>

然后你只需要删除前置条件属性。否则,将不会为静态文件(以及可能不会调用托管管道的任何其他内容)调用您的模块。

【讨论】:

  • 没有前置条件。我在上面更新了我的 web.config 的样子
  • 我希望你现在已经解决了这个问题,但我怀疑我需要更多关于你在做什么的信息。一般来说,我概述的方法应该有效。您可能可以运行失败的请求跟踪模块来跟踪 IIS 实际在做什么。
猜你喜欢
  • 2011-09-20
  • 2016-09-20
  • 2018-05-04
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
相关资源
最近更新 更多