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
4

1 に答える 1

2

_current 変数は実際にはありHttpContext.Currentますか? モジュール内の静的フィールドですか? いつ/どのように初期化されますか? 私の推測では、空の catch 句がすべてのエラーを飲み込んでしまい、その考えに従うと、おそらく _current で null 参照を取得することになります。コードの問題点を詳しく知るには、try/catch を削除してみてください

于 2011-07-18T12:01:24.583 に答える