过滤是不允许的 [英] Filtering is not allowed

查看:89
本文介绍了过滤是不允许的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个动作方法来压缩HTML它给异常过滤不允许我已经在网上搜索,但没有找到任何合适的解决方案。请指导我这个这个问题将如何我可以解决。我在我的共享code:

I have implemented a action method to minify HTML it is giving exception "filtering is not allowed" i have searched the internet but couldn't find any suitable solution. please guide me with this how I this issue will be solved. I'm sharing my code:

MinifyAttribute类:

MinifyAttribute Class:

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


    }

我打电话这种方法的控制器并获得异常

推荐答案

您code似乎是在一个品牌使用时是为我工作在VS2013(普通前$ P $创建新的ASP.NET MVC应用程序5 pssions可能需要调整,但是这是一个小细节)。我已经上传了一个完整的解决方案这里。你可以尝试一下吗?

Your code seems to be working for me when used in a brand new ASP.NET MVC 5 application created in VS2013 (the regular expressions may need to be adjusted, but that's a minor detail). I've uploaded a full solution here. Could you try it out?

需要明确的是,我注意到您已经标记了问题asp.net-MVC-3和asp.net-MVC-4,但我还没有机会测试这些版本的ASP.NET MVC。

To be clear, I noticed that you've tagged the question with asp.net-mvc-3 and asp.net-mvc-4, but I haven't had the opportunity to test on those version of ASP.NET MVC.

这篇关于过滤是不允许的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆