IIS7 模块只能在第一次使用吗?

发布于 2024-11-24 07:01:07 字数 1605 浏览 0 评论 0原文

我创建了一个 IIS 模块,该模块在加载之前将文本附加到页面。当我访问该 URL 时,页面第一次加载时效果非常好。然而,在后续加载中,永远不会附加文本。

关于如何解决这个问题有什么想法吗?

== 代码 ==

这是我的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.webServer>
        <modules>
            <add name="MIModule" type="MI.MyModule, MI" />
        </modules>
        <caching enabled="false" enableKernelCache="false" />       
    </system.webServer>
</configuration>

一些模块代码:

public void context_PreRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication app = (HttpApplication)source;
            HttpRequest request = app.Context.Request;

            string pageContent = app.Response.Output.ToString();

            string useragent = "HI!<br />" + pageContent + "<hr />" ;

            try
            {
                _current.Response.Output.Write(useragent);
            }
            catch
            {
            }
        }

以及其余代码:

private HttpContext _current = null;

        #region IHttpModule Members

        public void Dispose()
        {
            throw new Exception("Not implemented");
        }

        public void Init(HttpApplication context)
        {
            _current = context.Context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        #endregion

I create an IIS Module that appends text to the page before it loads. When I go to the URL, this works perfect the first time the page loads. On subsequent loads, however, the text is never appended.

Any thoughts on how to remedy this?

== CODE ==

Here's my web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.webServer>
        <modules>
            <add name="MIModule" type="MI.MyModule, MI" />
        </modules>
        <caching enabled="false" enableKernelCache="false" />       
    </system.webServer>
</configuration>

Some module code:

public void context_PreRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication app = (HttpApplication)source;
            HttpRequest request = app.Context.Request;

            string pageContent = app.Response.Output.ToString();

            string useragent = "HI!<br />" + pageContent + "<hr />" ;

            try
            {
                _current.Response.Output.Write(useragent);
            }
            catch
            {
            }
        }

and the rest of the code:

private HttpContext _current = null;

        #region IHttpModule Members

        public void Dispose()
        {
            throw new Exception("Not implemented");
        }

        public void Init(HttpApplication context)
        {
            _current = context.Context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        #endregion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2024-12-01 07:01:07

_current 变量实际上是 HttpContext.Current 吗?它是您模块中的静态字段吗?它何时/如何初始化?我的猜测是,空的 catch 子句会吞噬所有错误,并且按照这个想法,您很可能会在 _current 上获得空引用。尝试删除 try/catch 以了解有关代码问题的更多信息

Is the _current variable actually HttpContext.Current? Is it a static field in your module? When/how is it initialized? My guess is that the empty catch clause gobbles up all errors, and following that thought, you most probably get a null reference on _current. Try removing the try/catch to learn more on what's wrong with your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文