ASP.NET HttpModule,使用 Response.Write 到当前上下文时出现奇怪的行为
我试图理解为什么这个非常简单的 HttpModule 失败了。该代码是我需要为测试项目开发的简单 HttpUrlRewriter 的前身。
看来每当我测试请求,然后执行响应时,输出都不会写入流!
我已将调试器 (VS 2008) 连接到模块,并且下面的所有 3 个 Response.Write 语句都会被执行,但只有两个外部语句实际上在页面上生成输出。我是否错过了一个关键的理解或警告?
感谢您的任何帮助。
执行环境:ASP.NET 3.5/WinXP/IIS 5
使用系统; 使用 System.Collections.Generic; 使用系统.Web; 使用系统文本; 使用 System.Web.UI;
public class Interceptor : IHttpModule
{
#region IHttpModule Members
public void Dispose() { }
public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(TestHandler);
}
private void TestHandler(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
if (1 == 1)
{
ctx.Response.Write("Hello, 2"); // Works, as expected
}
string test = ctx.Request.Url.ToString();
if (test.Contains("/images")) {
ctx.Response.Write("Hello, never written"); // This code executes when the test passes, but nothing is ever written...
}
ctx.Response.Write("Hello"); // Works
}
#endregion
}
I'm trying to understand why this very simple HttpModule fails. The code is a precursor to a simple HttpUrlRewriter that I need to develop for a test project.
It appears that whenever I test the Request, and then execute a Response, the output is not written to the stream!
I've attached the debugger (VS 2008) to the module, and all 3 Response.Write statements in the below get executed, but only the two outer ones actually product output on the page. Have I missed a key understanding or caveat?
Thanks for any help.
Exeucting Environment: ASP.NET 3.5/WinXP/IIS 5
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
public class Interceptor : IHttpModule
{
#region IHttpModule Members
public void Dispose() { }
public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(TestHandler);
}
private void TestHandler(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
if (1 == 1)
{
ctx.Response.Write("Hello, 2"); // Works, as expected
}
string test = ctx.Request.Url.ToString();
if (test.Contains("/images")) {
ctx.Response.Write("Hello, never written"); // This code executes when the test passes, but nothing is ever written...
}
ctx.Response.Write("Hello"); // Works
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您请求图像,响应流也会不同!
If your requesting an image the Response stream will be different too!
您确定您正在监视的请求已命中该行上的断点吗?请注意,如果 /images 文件夹中有一个图像以及包含该图像的 .aspx 页面,则您的模块将在开发服务器中调用两次(对于 aspx 和图像),并且在默认 IIS 中仅调用一次(仅对于 aspx 文件)。当您同时向模块发出请求时,有时很难看出哪个断点位于哪个请求上。
are you sure the breakpoint on that line is hit for the request you are monitoring? Be aware that if you have an image in that /images folder and a .aspx page that contains that image your module will be called twice in the development server (for the aspx and the image), and only once in a default IIS (only for the aspx file). When you have simultaneous requests to the module it's sometime hard to see which breakpoint is on which request.
可悲的是,我的答案是愚蠢的。那些被忽视的经典之一!
该请求虽然包含 /image,但也恰好是针对二进制文件。因此,响应被写入流而不是页面。
The answer was, sadly, stupidity on my part. One of those classics that get overlooked!
The request, while containing /image, also happened to be for a binary file. Therefore the Response was written to the stream for that rather than the Page.