我在 JsonNetResult(也在下面)的输出上使用 CompressFilter 属性(下面),一切正常,直到我将 CompressFilter 属性应用于我的控制器方法,然后我在响应中得到 0 内容长度。单步执行代码一切看起来都不错,我在 JsonNetResult 中点击了 response.Write 并使用了一个有效的字符串。
有谁知道我该如何找出问题所在?我很难过
using System.Collections;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
public class CompressFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
string[] supported = {"gzip", "deflate"};
IEnumerable<string> preferredOrder = new AcceptList(request.Headers["Accept-Encoding"], supported);
var preferred = preferredOrder.FirstOrDefault();
var response = filterContext.HttpContext.Response;
switch (preferred)
{
case "gzip":
response.AppendHeader("Content-Encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
break;
case "deflate":
response.AppendHeader("Content-Encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
break;
case "identity":
default:
break;
}
}
}
public class AcceptList : IEnumerable<string>
{
private readonly IEnumerable<string> encodings;
private readonly Regex parser = new Regex(@"(?<name>[^;,\r\n]+)(?:;q=(?<value>[\d.]+))?", RegexOptions.Compiled);
public AcceptList(string acceptHeaderValue, IEnumerable<string> supportedEncodings)
{
var accepts = new List<KeyValuePair<string, float>>();
if (!string.IsNullOrEmpty(acceptHeaderValue))
{
var matches = parser.Matches(acceptHeaderValue);
var values = from Match v in matches
where v.Success
select new
{
Name = v.Groups["name"].Value, v.Groups["value"].Value
};
foreach (var value in values)
{
if (value.Name == "*")
{
foreach (var encoding in supportedEncodings)
{
if (!accepts.Where(a => a.Key.ToUpperInvariant() == encoding.ToUpperInvariant()).Any())
{
accepts.Add(new KeyValuePair<string, float>(encoding, 1.0f));
}
}
continue;
}
var desired = 1.0f;
if (!string.IsNullOrEmpty(value.Value))
{
float.TryParse(value.Value, out desired);
}
if (desired == 0.0f)
{
continue;
}
accepts.Add(new KeyValuePair<string, float>(value.Name, desired));
}
}
encodings = from a in accepts
where supportedEncodings.Where(se => se.ToUpperInvariant() == a.Key.ToUpperInvariant()).Any() || a.Key.ToUpperInvariant() == "IDENTITY"
orderby a.Value descending
select a.Key;
}
IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
return encodings.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) encodings).GetEnumerator();
}
}
public class JsonNetResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data == null)
return;
var jsonSerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, DateFormatHandling = DateFormatHandling.IsoDateFormat };
var serializedObject = JsonConvert.SerializeObject(Data, jsonSerializerSettings);
response.Write(serializedObject);
}
}