Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering is not allowed

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 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

    }
}

Minify Class

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


    }

I'm calling this method on the Controller and getting the exception

like image 889
DevWithSigns Avatar asked May 17 '14 06:05

DevWithSigns


2 Answers

Try to add null-check before apply filter:

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

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

    response.Filter = new YourFilter(response.Filter);
}
like image 184
vladimir Avatar answered Oct 23 '22 06:10

vladimir


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?

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.

like image 42
Rune Avatar answered Oct 23 '22 06:10

Rune