ASP.NET MVC2 CrAzY Characters in View Output - WTF

我与影子孤独终老i 提交于 2019-12-08 17:19:15

问题


Every once in a while when I'm running my app I get really "REALLY" strange characters in my output instead of the appropriate page. Now I know that this is because of some error, but unfortunately the error doesn't present it's self, but rather just produces strange characters.

Here's an example of a complete page source code.

��������I�%&/m�{J�J��t��$ؐ@�����iG#)���eVe]f@�흼��{���{��;�N'���?\fdl��J�ɞ!���?~|?"��Ey�')=��y6����h���u����r���j�fŲIU���<[��2O�2_����]i�ߴ��餚]����l�g���~��O��,[f�Wyq1o-�z���QS� ����iUV�I�M���ԃ�z�>�EV_��Z=J�������T���������f�����Z��gi�r�k�ܷ �ZPW4�����,KO�eS��יy�/���m^+�E eB��c�j�w��,�Vu���Q�$n:@]�uC_��X_����E^��d�Tm[-��;�w�v�V�r�MJY��y���uYP祐���2�����uC����w}V|WI�d��\0��>��m���ւh����%y�i��)��X���d������jUS�z�x^�WŬ����󻧘��v�Ϛ�$G���^O��qq{�.�0�=��8��f�y6�?��.�r�~;��[Bt�~�/�K��z�|�-��W�ź��Q��&���4B��Q�4o�u��x|wrt�L�Kо$)��Ms�.��4-�ٺ.�4���]>]˷��7!4��IZc�M;N_�y����e�_q��%�LۚC�PE��9��e�j�J[^fe1�r���֏����p߯�uM�3�=B�È�,H�Y���sz�S̨��T�?��}��������k�Ⱊ���p��l�_d�̼/S��[V"p��}J�����pq^��!Z�<5���j��Wd�wc�O�䣏0 ���O/��h�jv��a�����}�J��y��E���zA�h@��45e�e�4?��e�u��vӆ��N����C�b���zE��!���UY��X�s�l���#��?}��Χm����/��u �I���уO��2[Lf����y5�:)�fM����(�Q���}��)!�������d���t|< �PO������$�ꀜ�?=૬���<���?/�q>���b�7��^��(={Z��Y}��u�=8�u��J��D�c���vt�O���܋����/� �����1ev}RfM3�/�~�h�ϊ ������-�}�����:�����a�\���lZ�<[/��Rv�5K(F������C�b�{;?�{�{;�?{^4- R��|��>�����[6���:���ps�FA�ʻ��7��ehU�+�R�>�0{�����܍��FI;�w�œ��2JӼi�r�>�po?j2��� �]���m��U1{J��/��,�C�������p^W�jm$��0^�7d������:�n��Vd��+�t9c-�x���ٹ��.�W�w�~�3�A�9�vۮ�-��M/=�>��R�������|�wǽy��Y����?8�����{�N��� ��#7��'���/�+�͋UIJa��fy�v�x��]x}?~��1s�u�� �!�p�]���4��/�i]5�y����I�A^U��T_{�?��۹���0=~������e�"�p�i���ﺽ�nr��k�����[z��{#����.��s�@#���M8| G�C�Y�Q7z���m/z������(>>�9Տ7:EG�N�g7?��=�������-�1�9�ir��z�������7vi4�x�76��v�>������z�v0~����3��zn�8����]/�H\� w�q�?�9���հk~3}��3��7�G:���ߤ�~��n���q��}���y�����Ō#�6�)��2\���lM���s �p^\��@�Vi3��Rr�'Uc�PDf��h�a�t��:�D�c���җ���E88�UHʹ�7�����j�*_Tm�岼֦4�U]�֬��|yYe� 7����'�����NJl,��

I have never seen this with web forms.

EDIT:

So I did some testing today and found some interesting stuff. First of all, the app was throwing a Null Reference Exception (500). Secondly the local debugger saw the error and threw a yellow screen of death right away. The bad text was being shown on the Staging server which currently runs a Release build.

So basically 500 errors on Release builds with customErrors off on my staging server throws CrAzY characters instead of failing gracefully to the yellow screen of death.

EDIT 2:

As per @Esteban's answer, here is my compression filter

Namespace Filters
    Public Class CompressFilter : Inherits ActionFilterAttribute
        ''' <summary>
        ''' GZip compresses each Action when loaded.  This satisfies YSlow and
        ''' PageSpeed.
        ''' </summary>
        ''' <param name="filterContext">The filter context.</param>
        Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
            Dim request As HttpRequestBase = filterContext.HttpContext.Request

            Dim acceptEncoding As String = request.Headers("Accept-Encoding")

            If String.IsNullOrEmpty(acceptEncoding) Then
                Return
            End If

            acceptEncoding = acceptEncoding.ToUpperInvariant()

            Dim response As HttpResponseBase = filterContext.HttpContext.Response

            If acceptEncoding.Contains("GZIP") Then
                response.AppendHeader("Content-encoding", "gzip")
                response.Filter = New GZipStream(response.Filter, CompressionMode.Compress)
            ElseIf acceptEncoding.Contains("DEFLATE") Then
                response.AppendHeader("Content-encoding", "deflate")
                response.Filter = New DeflateStream(response.Filter, CompressionMode.Compress)
            End If
        End Sub
    End Class
End Namespace

I'm setting this filter globally on every controller via a Base Controller. The page runs just fine if there are no other 500 errors, but if I have a 500 error peek it's ugly head, I get the funky characters.

Also, sometimes I don't have an error on my development server, but there is one on the Staging server. This happened recently when I didn't have the updated library on the staging server, but it was present on my Dev machine. I can see the errors in the Event Log, but I cannot see it on the web page... even if I set Custom Errors to Off.

EDIT 3:

Ok, so now we've discovered that this error comes because my <CompressionFilter()> doesn't decompress the stream if a 500 error is thrown. Gotta figure out how to decompress my output no matter what.


回答1:


This is what I did to get around the issue. I'm still trying to find a better way. Place the following code in the global.asax.cs

    protected void Application_PreSendRequestHeaders()
    {
        HttpResponse response = HttpContext.Current.Response;
        if (response.Filter is GZipStream && response.Headers["Content-encoding"] != "gzip")
            response.AppendHeader("Content-encoding", "gzip");
        else if (response.Filter is DeflateStream && response.Headers["Content-encoding"] != "deflate")
            response.AppendHeader("Content-encoding", "deflate");
    }

Update Check out the following link for some more information where Rick uses the following to solve the issue. Place the following in your global.asax.cs

protected void Application_Error(object sender, EventArgs e)
{
        // Remove any special filtering especially GZip filtering
        Response.Filter = null;
}

http://www.west-wind.com/weblog/posts/2011/May/02/ASPNET-GZip-Encoding-Caveats




回答2:


This looks like it might be an issue with invalid character set encoding being used on the response?

what encoding are you using? Unicode/UTF8 or an asian character set?




回答3:


No, it's not an invalid character set issue; I've had this happen before. What's happening is that you're deflating the content and somehow (either an exception occurs, you're forgetting to, etc) not setting the compression method you used in the headers.

Now, to actually solve the problem, you have a couple of options:

  1. On (on global.asax or in a custom handler) Application_PreSendRequestHeaders chek to see if the content is delfated and the headers are missing; you can either deflate the content or add the headers.
  2. On errors, deflate the content or add the correct headers.

Hope that helps.



来源:https://stackoverflow.com/questions/3183203/asp-net-mvc2-crazy-characters-in-view-output-wtf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!