IHttpModule处理经典的ASP表单数据
问题描述:
我有一个经典的ASP页面,我想在一些使用IHTTPModule的日志中打包。IHttpModule处理经典的ASP表单数据
我的问题是,如果我的模块在页面执行前访问任何表单变量,我的ASP页面在访问第一个Request.Form后立即返回错误“80004005”。
如果我把我的模块挂接到asp页面处理完成后发生的事件,那么httpApplication.Context.Request.Form集合是空的。
样本模块:
using System;
using System.Web;
namespace FormPostTest
{
public class MyModule1 : IHttpModule
{
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context)
{
/*
With this line (begin request) I get
error '80004005'
/index.asp, line 11 as soon as Request.Form is accessed from the ASP page, however the form collection
is populated.
*/
context.BeginRequest += context_GetFormParams;
/*
* The line causes for form collection to be empty
* */
// context.LogRequest += new EventHandler(context_GetFormParams);
}
private void context_GetFormParams(object sender, EventArgs e)
{
HttpApplication httpApplication = (HttpApplication) sender;
Console.WriteLine(httpApplication.Context.Request.Form.Get("MyFormParam"));
}
}
}
这是我的经典ASP页。
<html>
<head></head>
<body>
<form method="post" action="index.asp">
<input name="MyFormParam" value="Hello" />
<input type="submit" />
</form>
</body>
</html>
<%=Request.form("MyFormParam")%>
答
显然,(我不知道为什么)访问Form
使ASP.NET“消费” HTTP请求的身体;并且它不再能够被传统的ASP访问。
此处可能的解决方法是使用Server.TransferRequest
,特别是preserveForm
为true。这会导致服务器端传输;客户不会注意到。
由于这种效果会导致服务器像处理新请求一样处理传输的请求,并且由于推测您希望传输到相同的路径,因此您的IHttpModule
也会针对第二个“虚拟”请求执行。
这意味着您需要添加模块可以查找的自定义标题,以便可以禁止对第二个请求进一步处理。