7

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.

11
  • whoooooah....my brain is hemorrhaging!! Commented Jul 6, 2010 at 2:51
  • is it particular to one route? Are you doing any manual response.write stuff? Commented Jul 6, 2010 at 3:41
  • seems to be happening on one route, no response.write Commented Jul 6, 2010 at 3:49
  • 3
    Try hitting the URL via Fiddler Commented Jul 6, 2010 at 4:30
  • IE: is actually saying "unable to download 1 from staging.local - where "1" is the user ID in the route. Commented Jul 6, 2010 at 4:42

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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?

2 Comments

Have you tried explicitly encoding your response in UTF-8? this will force the browser to produce utf-8 documents in your pages. Also check your DOCTYPE as well -- are you using the appropriate doctypes for your control views?
I'm using the HTML5 DocType <!DOCTYPE html>
1

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.

6 Comments

I've also seen this happen. For some reason, when an un-handled exception is thrown, and ASP.NET has to handle the error, it clears the compression method from the header. I don't know why, but I've seen it happen. You should be able to duplicate this, by throwing an exception after your filter runs. You'll see the same output.
this makes sense, but how do I fix it?
This is exactly what the problem is. I removed the compression and threw an error. I Got YSOD. Added the compression filter back in... crazy characters came back. Now to figure out how to decompress even on error.
Yeah, Fiddler will confirm what you're seeing; you'll see compressed content but missing headers. The browser doesn't know it needs to de-compress.
Yeah, I get that. But other than turning off my CompressionFilter, I don't know how to avoid it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.