2

我有一个经典的 ASP 页面,我想使用 IHTTPModule 将其包含在一些日志记录中。

我的问题是,如果我的模块在页面执行之前访问任何表单变量,那么只要访问第一个 Request.Form,我的 ASP 页面就会返回错误“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")%>
4

1 回答 1

1

显然(我不知道为什么)访问Form会导致 ASP.NET “消耗” http 请求的主体;并且经典 ASP 不再可以访问它。

一种可能的解决方法是使用Server.TransferRequest,特别是使用preserveFormtrue。这会导致服务器端传输;客户不会注意到。

由于该有效导致服务器处理传输的请求,就好像它是新的一样,并且由于您想传输到相同的路径,因此您IHttpModule还将执行第二个“虚拟”请求。

这意味着您需要添加一个模块可以查找的自定义标头,以便它可以抑制对第二个请求的进一步处理。

于 2015-04-20T12:43:19.747 回答