locked
HTML contains multiple 'null' - hex 0 characters RRS feed

  • Question

  • User-725576607 posted

    In a site that has been working without any problems for some time, the resulting HTML sent to the browser contains multiple blocks of hex 0 characters. They are randomly inserted into the correct HTML for no apparent reason. They appear in the middle of URLs, comments and other places.

    It just started happening on my dev machine (Windows 10), and also on my Surface both using VS Community 2019, Version 16.1.1

    Other MVC sites in the same codebase and on the same machines run fine without the hex 0 characters.

    I tried reinstalling Visual Studio but that didn't fix it.

    Any ideas as to where the problem could be?

    Wednesday, May 29, 2019 11:24 PM

All replies

  • User1520731567 posted

    Hi Burstock,

    Could you please post your code so that I can reproduce your issue,

    And confirm if this is related to development.

    Best Regards.

    Yuki Tao

    Friday, May 31, 2019 3:02 AM
  • User-725576607 posted

    I have narrowed it down to an Action Filter which replaces 'http:' with '':

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
    var response = filterContext.HttpContext.Response;
    var f = response.Filter;
    response.Filter = new ReplaceTokensFilter(f, s =>
    {
    s = s.Replace("http:", string.Empty);
    return s;
    });
    }
    base.OnResultExecuted(filterContext);
    }

    public class ReplaceTokensFilter : Stream
    {
    private readonly Stream _stream;
    private readonly Func<string, string> _filter;
    public ReplaceTokensFilter(Stream shrink, Func<string, string> filter)
    {
    _stream = shrink;
    _filter = filter;
    }
    public override bool CanRead { get { return true; } }
    public override bool CanSeek { get { return true; } }
    public override bool CanWrite { get { return true; } }
    public override void Flush() { _stream.Flush(); }
    public override long Length { get { return 0; } }
    public override long Position { get; set; }
    public override int Read(byte[] buffer, int offset, int count)
    {
    return _stream.Read(buffer, offset, count);
    }
    public override long Seek(long offset, SeekOrigin origin)
    {
    return _stream.Seek(offset, origin);
    }
    public override void SetLength(long value)
    {
    _stream.SetLength(value);
    }
    public override void Close()
    {
    _stream.Close();
    }
    public override void Write(byte[] buffer, int offset, int count)
    {
    // capture the data and convert to string
    byte[] data = new byte[count];
    Buffer.BlockCopy(buffer, offset, data, 0, count);
    string s = Encoding.Default.GetString(buffer);

    // filter the string
    s = _filter(s);

    // write the data to stream
    byte[] outdata = Encoding.Default.GetBytes(s);
    _stream.Write(outdata, 0, outdata.GetLength(0));
    }
    }

    In the Write method of the ReplaceTokensFilter, the buffer length is 3075, the offset is 0 and the count is 1024.
    The buffer is padded with hex 0 in every slot past 1024.

    I can't see why the buffer is bigger than count.

    Sunday, June 2, 2019 7:35 PM
  • User-725576607 posted

    Never mind - found the issue:

    string s = Encoding.Default.GetString(buffer);

    should be:

    string s = Encoding.Default.GetString(data);

    Strange that it works OK on the production servers. Oh well.

    Sunday, June 2, 2019 7:49 PM
  • User1520731567 posted

    Hi Burstock,

    I'm glad that you have solved the issue by yourself.

    I suggest you could mark the right reply as answer.

    This will help other people who faces the same issue.

    Thank you very much.

    Best Regards.

    Yuki Tao

    Monday, June 3, 2019 1:42 AM