不允许过滤

时间:2014-05-17 06:06:58

标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我已经实现了一种操作方法来缩小HTML,它提供了异常“不允许过滤”我已经搜索了互联网但却找不到合适的解决方案。请指导我这个问题将如何解决。我正在分享我的代码:

MinifyAttribute类:

public class MinifyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        var request = filterContext.HttpContext.Request;
        var response = filterContext.HttpContext.Response;

        response.Filter = new Minify(response.Filter, s =>
        {
            s = Regex.Replace(s, @"\s+", " ");
            s = Regex.Replace(s, @"\s*\n\s*", "\n");
            s = Regex.Replace(s, @"\s*\>\s*\<\s*", "><");
            s = Regex.Replace(s, @"<!--(.*?)-->", "");   //Remove comments

            var firstEndBracketPosition = s.IndexOf(">");
            if (firstEndBracketPosition >= 0)
            {
                s = s.Remove(firstEndBracketPosition, 1);
                s = s.Insert(firstEndBracketPosition, ">");
            }
            return s;
        }); // i'm getting exception here on this code block

    }
}

缩小班级

public class Minify : Stream
    {
        private Stream _shrink;
        private Func<string, string> _filter;

        public Minify(Stream shrink, Func<string, string> filter)
        {
            _shrink = shrink;
            _filter = filter;
        }


        public override bool CanRead { get { return true; } }
        public override bool CanSeek { get { return true; } }
        public override bool CanWrite { get { return true; } }
        public override void Flush() { _shrink.Flush(); }
        public override long Length { get { return 0; } }
        public override long Position { get; set; }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return _shrink.Read(buffer, offset, count);
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return _shrink.Seek(offset, origin);
        }
        public override void SetLength(long value)
        {
            _shrink.SetLength(value);
        }
        public override void Close()
        {
            _shrink.Close();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            // capture the data and convert to string 
            byte[] data = new byte[count];
            Buffer.BlockCopy(buffer, offset, data, 0, count);
            string s = Encoding.Default.GetString(buffer);

            // filter the string
            s = _filter(s);

            // write the data to stream 
            byte[] outdata = Encoding.Default.GetBytes(s);
            _shrink.Write(outdata, 0, outdata.GetLength(0));
        }


    }

我在Controller上调用此方法并获取异常

4 个答案:

答案 0 :(得分:11)

尝试在应用过滤器之前添加空检查:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var response = filterContext.HttpContext.Response;

    if (response.Filter == null) return; // <-----

    response.Filter = new YourFilter(response.Filter);
}

答案 1 :(得分:3)

在VS2013中创建的全新ASP.NET MVC 5应用程序中使用时,您的代码似乎对我有用(正则表达式可能需要调整,但这是一个小细节)。我上传了完整的解决方案here。你能尝试一下吗?

要清楚,我注意到你用asp.net-mvc-3和asp.net-mvc-4标记了这个问题,但是我没有机会测试那些版本的ASP.NET MVC。

答案 2 :(得分:0)

我遇到了同样的问题w /你的自定义过滤器和我的,我对Rune的答案(接受的答案)不满意。当我偶然发现web page时,我能够确定问题。

select topics_id 
from t_topics_header a
  join t_topics_group b using (topics_group_id) 
where (a.subject ilike '%aaa%' or a.contents ilike '%aaa%' 
   or a.topics_group_id = 7 )
  and query_columns(a) ilike ='%aaa%';

答案 3 :(得分:0)

这可能无关紧要,但是我登陆这里搜索“不允许过滤”错误。我安装了Visual Studio 2019之后就发生了这件事(当我收到错误时正在VS 2017中调试我们的项目)。

原来,我不得不将项目从.NET Framework 4.7.1升级到.NET Framework 4.8。

此后,我不再收到错误。另外,我们的项目也可以在Visual Studio 2019中使用,因此我们获得了升级Visual Studio的额外好处。