简体   繁体   English

在ASP classic中使用VBScript重定向响应输出

[英]Redirect response output with VBScript in ASP classic

In a plain .asp file, any content outside of <% %> tags is sent directly to the output buffer. 在简单的.asp文件中, <% %>标记之外的任何内容都将直接发送到输出缓冲区。 Additionally, an expression in <%= %> tags is evaluated and sent to the output buffer. 此外,计算<%= %>标记中的表达式并将其发送到输出缓冲区。

I want to redirect it so that, in some context that I establish, the result of those two constructs is instead sent to a buffer that I control. 我想重定向它,以便在我建立的某些上下文中,这两个构造的结果被发送到我控制的缓冲区。 If possible, I'd like to be able to do this dynamically, so that I can redirect the output to different buffers at runtime. 如果可能的话,我希望能够动态地执行此操作,以便我可以在运行时将输出重定向到不同的缓冲区。

The problem is open-ended, largely because I'll be planning what I'm building around this solution. 问题是开放式的,很大程度上是因为我将计划围绕这个解决方案构建的东西。 I could use any way that exists to capture this output. 我可以使用任何存在的方式来捕获此输出。 Performance and ease of use are not major considerations. 性能和易用性不是主要考虑因素。

This is a sequel to this question , in which I try one possible solution that turns out to not work. 这是这个问题的续集,我尝试了一种可能的解决方案,结果证明不起作用。

This isn't a real solution, but it's the best I can come up with: 这不是一个真正的解决方案,但它是我能想到的最好的解决方案:

    <% sub foo %>
            <h1>Hello, World!</h1>
    <% end sub %>

Then the sub's contents can be written to the Response if and when they are needed. 然后,如果需要,可以将子内容写入响应。 The sub can be manipulated using GetRef. 可以使用GetRef操作子。 This will pollute the global namespace with subs; 这将使用subs污染全局命名空间; they can't even be methods since GetRef won't work with them. 它们甚至不能成为方法,因为GetRef不能与它们一起使用。 (The first time I tested this, I accidentally stomped on an existing function.) (我第一次测试这个,我不小心踩了一个现有的功能。)

I'd really like to improve on this, if it's possible. 如果有可能的话,我真的想改进这个。 I'd appreciate any feedback. 我很感激任何反馈。

I don't think that you can do this in ASP. 我认为你不能在ASP中做到这一点。 You could try to create an ISAPI filter that will monitor the response for some placeholders that you put in there an do something different with the data between the placeholders. 您可以尝试创建一个ISAPI筛选器,该筛选器将监视您放入其中的某些占位符的响应,并对占位符之间的数据执行不同的操作。
I am not sure exactly what your end goal is so I do not know if this would work for you. 我不确定你的最终目标究竟是什么,所以我不知道这对你是否有用。

In effect, you are trying to rewrite the asp.dll ISAPI filter. 实际上,您正在尝试重写asp.dll ISAPI筛选器。 If you really wanted to do this, you could try to write a wrapper DLL around the asp.dll which overrides its WriteClient function. 如果你真的想这样做,你可以尝试在asp.dll周围编写一个包装器DLL来覆盖它的WriteClient函数。 This will not be straightforward and begs the question of what you trying to accomplish. 这不是直截了当的,而是在想你要完成什么的问题。

A far simpler solution would be to avoid using <% %> altogether and instead assemble the entire page (or most of it) in code so that your page is nothing more than: 一个更简单的解决方案是避免完全使用<% %> ,而是在代码中组装整个页面(或大部分),这样您的页面就不过是:

<html>
<%=OutputHtml()%>
</html>

In this way, you can have total control over what gets outputted and when. 通过这种方式,您可以完全控制输出的内容和时间。

well for the <%=%> thing you could write your own function: 那么<%=%>你可以编写自己的函数:

function [=](val)
    response.write val
    ' do anything you want with val
end function

the other thing is not possible i think. 我认为另一件事是不可能的。 but you could use the <%=%> construct for every output you want to redirect... 但您可以为要重定向的每个输出使用<%=%>构造...

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

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