Asp.net mvc 4 ASP.NET MVC4中的压缩筛选器导致内容长度为0
我在JSonnerResult(也在下面)的输出上使用CompressFilter属性(下面),在我将CompressFilter属性应用于我的控制器方法,然后在响应中得到0内容长度之前,一切都正常工作。在代码中,一切看起来都正常,我点击了响应。使用有效字符串写入JsonNetResult。Asp.net mvc 4 ASP.NET MVC4中的压缩筛选器导致内容长度为0,asp.net-mvc-4,attributes,compression,Asp.net Mvc 4,Attributes,Compression,我在JSonnerResult(也在下面)的输出上使用CompressFilter属性(下面),在我将CompressFilter属性应用于我的控制器方法,然后在响应中得到0内容长度之前,一切都正常工作。在代码中,一切看起来都正常,我点击了响应。使用有效字符串写入JsonNetResult。 有人知道我怎么才能找出问题所在吗?我被难住了 using System.Collections; using System.Collections.Generic; using System.IO.Comp
有人知道我怎么才能找出问题所在吗?我被难住了
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);
}
}
使用系统集合;
使用System.Collections.Generic;
使用系统IO压缩;
使用System.Linq;
使用System.Text.RegularExpressions;
使用System.Web.Mvc;
公共类CompressFilterAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
var request=filterContext.HttpContext.request;
支持的字符串[]={“gzip”,“deflate”};
IEnumerable preferredOrder=新的AcceptList(请求头[“接受编码”],受支持);
var preferred=preferredOrder.FirstOrDefault();
var response=filterContext.HttpContext.response;
开关(首选)
{
案例“gzip”:
AppendHeader(“内容编码”、“gzip”);
response.Filter=newgzipstream(response.Filter,CompressionMode.Compress);
打破
案例“deflate”:
AppendHeader(“内容编码”,“deflate”);
response.Filter=新的DeflateStream(response.Filter,CompressionMode.Compress);
打破
案例“身份”:
违约:
打破
}
}
}
公共类接受列表:IEnumerable
{
私有只读IEnumerable编码;
私有只读正则表达式解析器=新正则表达式(@“(?[^;,\r\n]+)(?:;q=(?[\d.]+))?”,RegexOptions.Compiled);
公共接受列表(字符串acceptHeaderValue,IEnumerable supportedEncodings)
{
var accepts=newlist();
如果(!string.IsNullOrEmpty(acceptHeaderValue))
{
var matches=parser.matches(acceptHeaderValue);
var值=来自匹配项中的匹配项v
成功在哪里
选择新的
{
Name=v.Groups[“Name”].Value,v.Groups[“Value”].Value
};
foreach(值中的var值)
{
如果(value.Name==“*”)
{
foreach(supportedEncodings中的var编码)
{
如果(!accepts.Where(a=>a.Key.ToUpperInvariant()==encoding.ToUpperInvariant()).Any())
{
接受.Add(新的KeyValuePair(编码,1.0f));
}
}
持续
}
所需var=1.0f;
如果(!string.IsNullOrEmpty(value.value))
{
float.TryParse(value.value,超出所需值);
}
如果(需要==0.0f)
{
持续
}
Add(新的KeyValuePair(value.Name,所需));
}
}
encodings=从中的
where supportedEncodings.where(se=>se.ToUpperInvariant()==a.Key.ToUpperInvariant()).Any()| | a.Key.ToUpperInvariant()
orderby a.值递减
选择a.键;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回编码。GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回((IEnumerable)编码);
}
}
公共类JsonNetResult:JsonResult
{
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
if(上下文==null)
抛出新的ArgumentNullException(“上下文”);
var response=context.HttpContext.response;
response.ContentType=!String.IsNullOrEmpty(ContentType)?ContentType:“应用程序/json”;
if(ContentEncoding!=null)
response.ContentEncoding=ContentEncoding;
如果(数据==null)
回来
var jsonSerializerSettings=new jsonSerializerSettings{Formatting=Formatting.Indented,DateFormatHandling=DateFormatHandling.IsoDateFormat};
var serializedObject=JsonConvert.SerializeObject(数据,jsonSerializerSettings);
response.Write(序列化对象);
}
}