我在ASP.NET MVC 3中有一個有趣的問題,即gzip/deflate ActionFilterAttribute。如果我的應用程序引發異常,而不是獲取YSOD,如下所示的亂碼。當拋出異常時gzip/deflate失敗
I�%&/m�{J�J��t��
$ @ iG#) * eVe] f @ 흼 { { ; N' ?\fdlJɞ〜|!?「安永」)= y6hZ2kjuU? + _x- : T W v < [ 〜2 g 2 ? ʋ y hYՋ t _N M l { , Xn Q } *g 7 〜�j'u>K�{_��IW4�>�U�w�|=-fYzR-�������|��<&�o�Z()*�S!U��k�g�������j��.����b}��ή�9X/��J�Iն��Q���z�i�n�-g٤���ݞ��Y^����H�8/��k�}]7�ǜ@�{|�g��wUd�O����죫y���o-�����ݏ��� �ZHv,�d]��١�>o3�=�3x�7MN�����������Ow���w�.o��φ�<؟M����;���vg���A>��䋟{YޟN�����Φ�$p>q����/�!�y��9�2��two������?������Ӈ���n�9�r�^����!������{���ag�?\1*c�?!�bي
? xI u f ? {' P$ v& =#s l _0 w ss 廌⼽[R![KP \ 7M ( 4 ߛ> > @「| |v y5 QꆦR JSK & ߛ p v< C t 1 hOI y{j ]i ˷ D'p< $,�'M��r{-�}��CF�؛�����A��9��[�½�� �! 2�� �:��!��{�t�;�߇'y��M��+�M^#x^\����Q��jM�l��?(�]� ��IZ�ݟ[����+4#"�:�X����m�������dv>������iL�̀I |�fL�TU��ho�� �{L��_t��5�o?���h�O�UY]#�u�[���G�ޞ�=���;��8���~����d�8k�w�����yw�
ֺ Nx A [XMO F /噩; Y〜
如果我刪除我的CompressAttribute
,它按預期工作(我看到了YSOD)!因此,我的異常處理(來自Elmah.Contrib.Mvc的ElmahHandleErrorAttribute
)似乎會暫停其餘的過濾器,包括CompressAttribute
,並且響應不會縮減。
相關代碼:
public sealed class CompressAttribute : ActionFilterAttribute
{
private const string _acceptEncodingHeader = "Accept-Encoding";
private const string _contentEncodingHeader = "Content-Encoding";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers[_acceptEncodingHeader];
if (String.IsNullOrEmpty(acceptEncoding))
{
return;
}
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader(_contentEncodingHeader, "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader(_contentEncodingHeader, "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
註冊過濾器:
GlobalFilterCollection filters = GlobalFilters.Filters;
filters.Add(new ElmahHandleErrorAttribute(), 999); // Elmah.Contrib.Mvc
filters.Add(new CompressAttribute());
我怎樣才能確保響應是可讀的,即使拋出異常?
我也打開其他/更好的方法來啓用http壓縮。 – jrummell 2012-02-14 19:04:56
你看過IIS內置的動態壓縮嗎? – 2012-02-14 19:37:06
@amit_g我沒有。你有鏈接嗎? – jrummell 2012-02-14 19:49:46