如何修改Request.InputStream

时间:2012-06-06 04:44:34

标签: asp.net sharepoint filter inputstream httpmodule

我必须修改InputStream 我知道我们可以使用带有自定义Filter的自定义HttpModule来实现。我还创建了一个Filter,它可以工作:它可以改变InputStream 我的过滤器源代码是:

namespace Test.Filter
{
    public class TestRequestFilter : Stream
    {
        private readonly Stream _inputStream;

        public TestRequestFilter (Stream inputFilter)
        {
            _inputStream = inputFilter;
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            _outputStream.Read(buffer, 0, (int)_outputStream.Length);
            var oldText = buffer.ToDefaultEncodingString();
            var newText = "hello world" + oldText;

            Encoding.Default.GetBytes(newText, 0, Encoding.Default.GetByteCount(newText), buffer, 0);

            return buffer.Length;
        }

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override long Length
        {
            get { return _inputStream.Length; }
        }

        public override long Position
        {
            get { return _inputStream.Position; }
            set { throw new NotSupportedException(); }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }

        public override void Flush()
        {
            _inputStream.Flush();
        }

        public override void Close()
        {
            _inputStream.Close();
        }
    }
}

我的问题有时候过滤器似乎没有运行(不调用Filter的Read方法) 我的问题是:过滤器什么时候运行? 我的环境:window 2008,.net 3.5,IIS 7.5,SharePoint 2010。

所有回复都将不胜感激。

0 个答案:

没有答案