我创建了一个HttpModule
:
using System;
using System.Web;
public class TestModule : IHttpModule
{
public TestModule() { }
public String ModuleName { get { return "TestModule"; } }
public void Dispose() { }
public void Init(HttpApplication app)
{
app.BeginRequest += (new EventHandler(this.DoBeginRequest));
}
private void DoBeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<pre>Request URL: " + context.Request.FilePath + "</pre>");
context.Response.Flush();
System.Diagnostics.Debug.WriteLine(context.Request.ToString());
}
}
这样加载:
<system.webServer>
<modules>
<add name="TestModule" type="TestModule"/>
</modules>
</system.webServer>
当我从 Web 浏览器或者使用 curl
命令调用这个函数时,输出面板中会打印两行日志信息,同时我也会看到下面的响应结果:
<pre>Request URL: /example</pre><pre>Request URL: /example</pre>
这表明每次都是相同的上下文。 为什么会发生这种情况?
Request
对象中有很多字段,但我还没有找到两个调用之间的任何区别。是否有某个属性应该被检查,以便给出一个“阶段”,我只回应其中一个?我在这个站点上找到了几个相关问题,但大多数似乎倾向于是Web浏览器寻找另一个资源,例如
favicon.ico
,而这里并非如此。
MSDN对于BeginRequest
的细节似乎不够详细,所以我到目前为止没有找到太多帮助。我可能漏掉了一些显而易见的东西,这是我第一次接触.NET/IIS等技术,通常我是一名Java开发人员。
更新:
我转储了Request
的所有公共属性,这是两者之间的区别:Headers == Accept=*%2f*&Host=localhost%3a2017&User-Agent=curl%2f7.35.0
Headers == Content-Length=0&Accept=*%2f*&Host=localhost%3a2017&User-Agent=curl%2f7.35.0
具体来说,在第二次调用时设置了
Content-Length
标头。 这在 curl
发出的请求中不存在。> GET /example HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:2017
> Accept: */*
这是IIS试图提供帮助吗?在处理完整个请求后,它知道正文的长度(0),然后再次调用带有该设置的函数吗?
DoBeginRequest
之前是[External code]
。 - Stik