I have a system which sits on a web server and generates files on the fly in response to HTTP requests. This is currently implemented as an HTTPHandler.
我有一个系统,它位于Web服务器上,可以动态生成文件以响应HTTP请求。目前这是作为HTTPHandler实现的。
Once the files are generated, they don't change very often, so I'd like to implement a cache.
生成文件后,它们不会经常更改,因此我想实现缓存。
Ideally, I'd like the web server to look at the cache folder and serve the files directly from there without any of my code having to execute (web servers are designed to be good at serving files after all, so if I can keep out of the way of that so much the better!).
理想情况下,我希望Web服务器查看缓存文件夹并直接从那里提供文件而不需要执行任何代码(Web服务器毕竟设计为擅长提供文件,所以如果我可以保留这样的方式更好!)。
What I'd like to then do is hook into the server's "file not found" event as an opportunity to create the file, drop a copy in the cache folder for the next time it's requested and also return it to the user instead of the "file not found" message.
我想做的是挂钩服务器的“找不到文件”事件,作为创建文件的机会,将副本放在缓存文件夹中以供下次请求,并将其返回给用户而不是“找不到文件”消息。
This way, repeat requests for files will be lightening fast and my code will only get called in 'exceptional' cases.
这样,对文件的重复请求将快速闪烁,我的代码将仅在“特殊”情况下被调用。
So - the question is - how do I wire my code into the "file not found" event in as unobtrusive and lightweight way as possible?
所以 - 问题是 - 如何以尽可能不引人注目和轻量级的方式将我的代码连接到“找不到文件”事件?
Thanks
2 个解决方案
#1
2
It's actually really simple, just point your 404 error page in IIS to your HTTPHandler.
它实际上非常简单,只需将IIS中的404错误页面指向HTTPHandler即可。
Request.RawUrl will look like:
Request.RawUrl看起来像:
http://yourdomain.com/yourhandler.ashx;originally/requested/url
#2
1
How about redirecting to your HttpHandler using the customError configuration.
如何使用customError配置重定向到您的HttpHandler。
<customErrors mode="On">
<error statusCode="404" redirect="FileGeneratorHandler.ashx" />
</customErrors>
The only issue would be whether the referring page is available to the handler.
唯一的问题是引用页面是否可供处理程序使用。
#1
2
It's actually really simple, just point your 404 error page in IIS to your HTTPHandler.
它实际上非常简单,只需将IIS中的404错误页面指向HTTPHandler即可。
Request.RawUrl will look like:
Request.RawUrl看起来像:
http://yourdomain.com/yourhandler.ashx;originally/requested/url
#2
1
How about redirecting to your HttpHandler using the customError configuration.
如何使用customError配置重定向到您的HttpHandler。
<customErrors mode="On">
<error statusCode="404" redirect="FileGeneratorHandler.ashx" />
</customErrors>
The only issue would be whether the referring page is available to the handler.
唯一的问题是引用页面是否可供处理程序使用。