با تشکر از مطلب ارسالی شما
برای اینکه فضای خالی به درستی حذف شود و همچنین تگ Pre هم در این الگوریتم لحاظ نشود. میتوان از اکشن فیلتر زیر استفاده کرد
public class RemoveWhitespacesAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { var response = filterContext.HttpContext.Response; if (filterContext.HttpContext.Request.RawUrl != "/sitemap.xml") { if (response.ContentType == "text/html" && response.Filter != null) { response.Filter = new HelperClass(response.Filter); } } } private class HelperClass : Stream { private System.IO.Stream Base; public HelperClass(System.IO.Stream ResponseStream) { if (ResponseStream == null) throw new ArgumentNullException("ResponseStream"); this.Base = ResponseStream; } StringBuilder s = new StringBuilder(); public override void Write(byte[] buffer, int offset, int count) { string HTML = Encoding.UTF8.GetString(buffer, offset, count); Regex reg = new Regex(@"(?<=\s)\s+(?![^<>]*</pre>)"); HTML = reg.Replace(HTML, string.Empty); buffer = System.Text.Encoding.UTF8.GetBytes(HTML); this.Base.Write(buffer, 0, buffer.Length); } #region Other Members public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } public override bool CanRead { get { return false; } } public override bool CanSeek { get { return false; } } public override bool CanWrite { get { return true; } } public override long Length { get { throw new NotSupportedException(); } } public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } } public override void Flush() { Base.Flush(); } public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public override void SetLength(long value) { throw new NotSupportedException(); } #endregion } }
protected void Application_Start() { try { GlobalFilters.Filters.Add(new App_Start.RemoveWhitespacesAttribute()); } catch { HttpRuntime.UnloadAppDomain(); // سبب ری استارت برنامه و آغاز مجدد آن با درخواست بعدی میشود throw; } }
برای Gzip هم اکثر در این حالت که هردو مورد با هم قرار داده شده است در برخی از موارد فایلهای جاواسکریپ را با مشکل روبرو میکند .به نظر من از Gzip توکار IIS استفاده شود بهتر است. البته باید ماژول آن در ISS فعال شده باشد.
برای اینکار هم داخل Web.config کدهای زیر را داخل configuration قرار بدید.
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"> <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" /> <dynamicTypes> <add mimeType="text/*" enabled="true" /> <add mimeType="message/*" enabled="true" /> <add mimeType="application/x-javascript" enabled="true" /> <add mimeType="application/javascript" enabled="true" /> <add mimeType="application/json" enabled="true" /> <add mimeType="application/json; charset=utf-8" enabled="true" /> <add mimeType="application/atom+xml" enabled="true" /> <add mimeType="application/xaml+xml" enabled="true" /> <add mimeType="*/*" enabled="false" /> </dynamicTypes> <staticTypes> <add mimeType="text/*" enabled="true" /> <add mimeType="message/*" enabled="true" /> <add mimeType="application/x-javascript" enabled="true" /> <add mimeType="application/javascript" enabled="true" /> <add mimeType="application/json" enabled="true" /> <add mimeType="application/json; charset=utf-8" enabled="true" /> <add mimeType="application/atom+xml" enabled="true" /> <add mimeType="application/xaml+xml" enabled="true" /> <add mimeType="*/*" enabled="false" /> </staticTypes> </httpCompression> <urlCompression doStaticCompression="true" doDynamicCompression="true" /> </system.webServer> <location path="Default Web Site"> <system.webServer> <serverRuntime enabled="true" frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" /> </system.webServer> </location>