简体   繁体   English

如何从IIS7和ASP.NET 4.0中的HTTP处理程序调用静态文件处理程序?

[英]How to call static file handler from HTTP handler in IIS7 and ASP.NET 4.0?

I create custom HTTP handler for auto generating file and I want to tell IIS7 to serve current request like normal static file because I need to use IIS and web.config file to control compression setting and any other HTTP header of current requested file. 我为自动生成文件创建自定义HTTP处理程序,我想告诉IIS7像普通静态文件一样提供当前请求,因为我需要使用IIS和web.config文件来控制压缩设置和当前请求文件的任何其他HTTP头。

I know. 我知道。 there is an internal class called [StaticFileHandler][1] in ASP.net. 在ASP.net中有一个名为[StaticFileHandler] [1]的内部类。 But I cannot access it. 但我无法访问它。 Or you have any other way to work like this. 或者你有任何其他方式可以像这样工作。 [1]: http://msdn.microsoft.com/en-us/library/ms404287.aspx [1]: http//msdn.microsoft.com/en-us/library/ms404287.aspx

It should be pretty easy to replicate StaticFileHandler 复制StaticFileHandler应该很容易

  1. Download Reflector 下载反射器
  2. Open System.Web 打开System.Web
  3. Search for StaticFileHandler 搜索StaticFileHandler
  4. Open method ProcessRequestInternal() Open方法ProcessRequestInternal()
  5. Open method SendFile() 打开方法SendFile()

Copy the bits you need, basically it boils down to this method: 复制你需要的位,基本上归结为这个方法:

context.Response.TransmitFile(physicalPath, offset, length);

This blog post describes when the StaticFileHandler will be used: 此博客文章描述了何时使用StaticFileHandler:

if you haven't modified any headers (such as caching) and you haven't already written something to the response stream , and the response stream doesn't have a filter (such as compression) and you're running in-process in IIS6 then the request is passed back to IIS for processing. 如果您尚未修改任何标头 (例如缓存)并且尚未向响应流写入内容,并且响应流没有过滤器 (例如压缩)并且您正在进行运行IIS6然后将请求传递回IIS进行处理。

As far as I've tested this is also true for IIS7. 据我所测试,这对IIS7也是如此。 I'm not exactly sure how, but if you don't handle a certain request in your own handler it should get passed on to IIS. 我不确定如何,但如果你不在自己的处理程序中处理某个请求,它应该传递给IIS。 Can anyone confirm that? 任何人都可以确认吗?

At least, following the above hints worked for my static files. 至少,遵循以上提示适用于我的静态文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM