【问题标题】:HttpModule, Response.Filter, and images not displayed未显示 HttpModule、Response.Filter 和图像
【发布时间】:2009-09-13 13:17:48
【问题描述】:

HttpModule 工作正常(“hello”被替换为“hello world”),但由于某些原因,当模块添加到 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”替换为“hello world”

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);
        }
}

【问题讨论】:

    标签: c# asp.net httpmodule


    【解决方案1】:

    您是否将该模块应用于所有请求?你不应该,因为它会弄乱任何二进制文件。您可能只是让您的事件处理程序仅在内容类型合适时应用过滤器。

    最好只将模块应用于特定的扩展来开始。

    老实说,您的流实现也有点狡猾 - 对于以 UTF-8 编码时占用多个字节的字符,它很可能会失败,并且即使只写入了部分缓冲区,您也正在解码整个缓冲区。此外,您可能会将“hello”拆分为“he”,然后是“llo”,这是您目前无法处理的。

    【讨论】:

    • 使事件处理程序在 .aspx 页面上应用过滤器只是这样做了。感谢您的帮助。
    【解决方案2】:

    尝试这样做,这只会为 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 或其他一些可以得到完美结果的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 2011-06-23
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      相关资源
      最近更新 更多