4

HttpModuleは正常に機能します(「hello」は「helloworld」に置き換えられます)が、モジュールがWeb.configに追加されたときに、何らかの理由でWebFormsの画像が表示されません。モジュールがWeb.configから削除されると、WebForms上の画像が表示されます。

誰かが理由を知っていますか?

HttpModuleの有無にかかわらず生成されるHTMLは、まったく同じです。

//The HttpModule

public class MyModule : IHttpModule
{
        #region IHttpModule Members

        public void Dispose()
        {
            //Empty
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
            application = context;
        }

        #endregion

        void OnBeginRequest(object sender, EventArgs e)
        {
            application.Response.Filter = new MyStream(application.Response.Filter);
        }
}

//フィルター-「hello」を「helloworld」に置き換えます

public class MyStream : MemoryStream
{
        private Stream outputStream = null;

        public MyStream(Stream output)
        {
            outputStream = output;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {

            string bufferContent = UTF8Encoding.UTF8.GetString(buffer);
            bufferContent = bufferContent.Replace("hello", "hello world");
            outputStream.Write(UTF8Encoding.UTF8.GetBytes(bufferContent), offset, UTF8Encoding.UTF8.GetByteCount(bufferContent));

            base.Write(buffer, offset, count);
        }
}
4

2 に答える 2

4

モジュールをすべてのリクエストに適用していますか?バイナリであるものを台無しにするので、そうすべきではありません。コンテンツタイプが適切な場合にのみ、イベントハンドラーにフィルターを適用させることができます。

ただし、最初はモジュールを特定の拡張機能にのみ適用する方がよいでしょう。

正直なところ、ストリームの実装も少し危険です。UTF-8でエンコードすると複数バイトを占める文字では失敗する可能性があり、バッファの一部だけが書き込まれている場合でもバッファ全体をデコードしています。さらに、「hello」が「he」に分割され、次に「llo」に分割される可能性がありますが、現在は対応していません。

于 2009-09-13T13:26:41.947 に答える
3

これを試してみてください。これにより、aspxページのフィルターのみがインストールされ、他のすべてのURLは正常に機能します。

void OnBeginRequest(object sender, EventArgs e)    
{
     if(Request.Url.ToString().Contains(".aspx"))        
         application.Response.Filter = new MyStream(application.Response.Filter);    
}

いくつかのプロパティがあります。完全な結果が得られるResponse.Url.AbsolutePathまたはその他のコードを使用する必要があります。

于 2009-09-13T13:24:36.483 に答える