C# ActionFilter响应类型(文本/html)与响应内容不匹配

C# ActionFilter响应类型(文本/html)与响应内容不匹配,c#,asp.net-mvc-5,action-filter,C#,Asp.net Mvc 5,Action Filter,使用:C#,MVC5,IIS8 我正在尝试实现一个ActionFilter,它将缩小html。这里的基本方法是用自定义流替换响应流,该自定义流将输入写入MemoryStream,然后在Close方法上缩小存储在MemoryStream中的内容并写出(缩小的)内容 我遇到的问题是,尽管响应的类型是“text/html”,但传递到自定义流的内容看起来不像文本或html,它看起来像二进制文件。我应该补充一点,我的站点页面呈现得很好,所以不管内容是什么,它都不是完全的垃圾。我添加了一些日志语句进行调试,

使用:C#,MVC5,IIS8

我正在尝试实现一个ActionFilter,它将缩小html。这里的基本方法是用自定义流替换响应流,该自定义流将输入写入MemoryStream,然后在Close方法上缩小存储在MemoryStream中的内容并写出(缩小的)内容

我遇到的问题是,尽管响应的类型是“text/html”,但传递到自定义流的内容看起来不像文本或html,它看起来像二进制文件。我应该补充一点,我的站点页面呈现得很好,所以不管内容是什么,它都不是完全的垃圾。我添加了一些日志语句进行调试,它们看起来是这样的:

缩小错误|块:1 | Url:/Login/iFrameLogin.aspx| 编码:System.Text.UTF8Encoding |媒体类型:Text/html |内容: �

我也尝试过在我的网站上关闭动态压缩,但也没有改变有人知道为什么我的“文本/html”看起来像二进制吗?

过滤特性

public class MinifyHtmlFilterAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        HttpContextBase context = filterContext.HttpContext;
        HttpRequestBase request = context.Request;
        HttpResponseBase response = context.Response;
        Encoding encoding = response.ContentEncoding;
        string mediaType = response.ContentType;
        string currentUrl = request.RawUrl;
        var minificationManager = HtmlMinificationManager.Current;

        if (response.Filter != null
            && response.StatusCode == 200
            && minificationManager.IsSupportedMediaType(mediaType) //text/html
            && minificationManager.IsProcessablePage(currentUrl))
        {
            response.Filter = new HtmlMinificationFilterStream(response, minificationManager, currentUrl, encoding, mediaType);
        }
    }
}
自定义流

public class HtmlMinificationFilterStream : Stream
{
    private readonly HttpResponseBase _response;
    private readonly Stream _stream;
    private readonly MemoryStream _cacheStream = new MemoryStream();

    private readonly IMarkupMinificationManager _minificationManager;

    private readonly string _currentUrl;
    private readonly Encoding _encoding;
    private readonly string _mediaType;
    private int _chunkCount = 0;

    public override bool CanRead
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override long Length
    {
        get { return 0; }
    }

    public override long Position
    {
        get;
        set;
    }

    public HtmlMinificationFilterStream(HttpResponseBase response,
        IMarkupMinificationManager minificationManager,
        string currentUrl,
        Encoding encoding,
        string mediaType)
    {
        _response = response;
        _stream = response.Filter;
        _minificationManager = minificationManager;
        _currentUrl = currentUrl;
        _encoding = encoding;
        _mediaType = mediaType;
    }

    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 Write(byte[] buffer, int offset, int count)
    {
        _cacheStream.Write(buffer, 0, count);
        _chunkCount++;
    }

    public override void Flush()
    {
        _stream.Flush();
    }

    public override void Close()
    {
        byte[] cacheBytes = _cacheStream.ToArray();
        int cacheSize = cacheBytes.Length;
        string content = _encoding.GetString(cacheBytes);
        var log = $" | Chunks: {_chunkCount} | Url: {_currentUrl} | Encoding: {_encoding} | MediaType: {_mediaType} | Content: {content}";

        IMarkupMinifier minifier = _minificationManager.CreateMinifier();
        MarkupMinificationResult minificationResult = minifier.Minify(content, _currentUrl, _encoding, false);
        bool isMinified = false;
        if (minificationResult.Errors.Count == 0)
        {
            ExceptionHandler.LogException("MINIFICATION SUCCESS" + log, System.Diagnostics.EventLogEntryType.Warning);
            using (var writer = new StreamWriter(_stream, _encoding))
            {
                writer.Write(minificationResult.MinifiedContent);
            }

            isMinified = true;
        }
        else
        {
            foreach (var error in minificationResult.Errors)
            {
                ExceptionHandler.LogException("Minification Error" + log + " | " + error.SourceFragment, System.Diagnostics.EventLogEntryType.Warning);
            }
        }

        if (!isMinified)
        {
            _cacheStream.Seek(0, SeekOrigin.Begin);
            _cacheStream.CopyTo(_stream);
        }

        _cacheStream.SetLength(0);
        _stream.Close();
    }
}

关闭IIS(8)上的压缩功能可以解决此问题。

您确定您的代码是最后执行的吗?也许二进制流是一种内部格式,之后还有另一个过程,通常会以不可见的方式将其转换回输出?最终找到了(某种程度上)-这是IIS压缩设置。当我关闭IIS上的压缩时,我的流看起来就像我期望的那样。