В фильтре WEBAPI im пытается рассчитать размер ответа. Аналогичный процесс работает для контроллеров MVC.Как получить длину ответа в ASP.NET Web API-контроллере?
Внутри actionExecutedContext.Response. Я не вижу фильтр?
Итак, я пробовал этот фильтр ниже, но это не работает.
Как я могу получить длину ответа WEBApi? Я мог бы использовать это в Global.ASAX, и он работает, но затем каждый HTTP-вызов регистрируется ... Таким образом, фильтр API был бы идеальным. Здесь что-то явно не так?
public class BosAPIFilter : System.Web.Http.Filters.ActionFilterAttribute{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) {
base.OnActionExecuted(actionExecutedContext);
var httpContext = actionExecutedContext.Request.Properties["MS_HttpContext"] as HttpContextWrapper;
if (httpContext != null) {
actionExecutedContext.Response.
httpContext.Response.Filter = new ResponseStreamHandler(httpContext.Response.Filter);
var handler = httpContext.Response.Filter as ResponseStreamHandler;
var adminService = new AdminServices();
adminService.HttpTrace(httpContext, handler);
}
}
public class ResponseStreamHandler : MemoryStream {
private readonly Stream _responseStream;
public long ResponseSize { get; private set; }
public ResponseStreamHandler(Stream responseStream) {
this._responseStream = responseStream;
ResponseSize = 0;
}
public override void Write(byte[] buffer, int offset, int count) {
this.ResponseSize += count;
this._responseStream.Write(buffer, offset, count);
}
// ReSharper disable once RedundantOverridenMember
public override void Flush() { base.Flush(); }
}