ASP. NET MVC page code compression Filter-Custom Delete invalid content

Source: Internet
Author: User
Tags httpcontext

ASP. NET MVC page code compression filter

The following filters are defined first for code compression.

/*page Compression Filters*/     Public classWhitespacefilter:stream {PrivateStream _shrink; Privatefunc<string,string>_filter;  PublicWhitespacefilter (Stream shrink, func<string,string>filter) {_shrink=shrink; _filter=filter; }         Public Override BOOLCanRead {Get{return true; } }         Public Override BOOLCanSeek {Get{return true; } }         Public Override BOOLCanWrite {Get{return true; } }         Public Override voidFlush () {_shrink. Flush (); }         Public Override LongLength {Get{return 0; } }         Public Override LongPosition {Get;Set; }  Public Override intRead (byte[] Buffer,intOffsetintcount) {            return_shrink.        Read (buffer, offset, count); }         Public Override LongSeek (Longoffset, SeekOrigin origin) {            return_shrink.        Seek (offset, origin); }         Public Override voidSetLength (Longvalue) {_shrink.        SetLength (value); }         Public Override voidClose () {_shrink.        Close (); }         Public Override voidWrite (byte[] Buffer,intOffsetintcount) {            //capture the data and convert to string            byte[] data =New byte[Count]; Buffer.blockcopy (buffer, offset, data,0, Count); strings = encoding.getencoding ("gb2312").            GetString (buffer); //Filter the strings =_filter (s); //write the data to stream            byte[] Outdata = encoding.getencoding ("gb2312").            GetBytes (s); _shrink. Write (Outdata,0, Outdata. GetLength (0)); }    }     Public classWhitespacefilterattribute:actionfilterattribute { Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {varRequest =filterContext.HttpContext.Request; varResponse =FilterContext.HttpContext.Response; Response. Filter=NewWhitespacefilter (response. Filter, S = ={s= Regex.Replace (S,@"\s+ (?=<) |\s+$| (?<=>) \s+|<!--. +-->| []+//.+\n|/\*.+\*/|\n",""); //Single-line doctype must be preserved                varFirstendbracketposition = S.indexof (">"); if(Firstendbracketposition >=0) {s= S.remove (Firstendbracketposition,1); S= S.insert (Firstendbracketposition,">"); }                returns;        }); }    }    /*page Compression filter End*/

Used on the action to compress the page.

[Whitespacefilter]  Public ActionResult Home () {      return  View ();}

We can modify the substitution rules on our own to achieve optimization.

@" \s+ (?=<) |\s+$| (?<=>) \s+|<!--. +-->| []+//.+\n|/\*.+\*/|\n""];

ASP. NET MVC page code compression Filter-Custom Delete invalid content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.