public override void OnException(HttpActionExecutedContext context) { var exception = context.Exception as MorpherException ?? new ServerException(context.Exception); var format = context.Request.GetQueryString("format"); if (format == null) { format = context.Request.GetHeader("ContentType"); if (format == null) { format = "xml"; } else if (format.Contains("application/json")) { format = "json"; } else if (format.Contains("application/xml")) { format = "xml"; } } var response = new ServiceErrorMessage(exception); context.Response = new HttpResponseMessage(HttpStatusCode.BadRequest); if (!context.Response.Headers.Contains("Error-Code")) { context.Response.Headers.Add("Error-Code", new[] { response.Code.ToString() }); context.Response.StatusCode = exception.ResponseCode; } switch (format) { case "json": context.Response.Content = new StringContent( JsonConvert.SerializeObject(response, Formatting.Indented)); break; case "xml": default: DataContractSerializer contractSerializer = new DataContractSerializer(typeof(ServiceErrorMessage)); // Если писать поток в StreamContent ничего не выводит // Если из ByteArray то кривая кодировка. using (StringWriter stringWriter = new StringWriter()) { using (XmlTextWriter textWriter = new XmlTextWriter(stringWriter)) { textWriter.Formatting = System.Xml.Formatting.Indented; contractSerializer.WriteObject(textWriter, response); context.Response.Content = new StringContent(stringWriter.GetStringBuilder().ToString()); } } break; } }
public override async Task Invoke(IOwinContext context) { string method = $"{context.Request.Method.ToLowerInvariant()}:{context.Request.Path.ToString().ToLowerInvariant()}"; if (_attributeUrls.Urls.TryGetValue(method, out _throttleThisAttribute)) { ApiThrottlingResult result; switch (_throttleThisAttribute.Mode) { case TarificationMode.PerRequest: result = _apiThrottler.Throttle(context.Request, _throttleThisAttribute.Cost); break; case TarificationMode.PerSymbol: result = PerSymbol(context.Request); break; case TarificationMode.PerWord: result = PerWord(context.Request); break; default: throw new ArgumentOutOfRangeException(); } if (result != ApiThrottlingResult.Success) { var exception = result.GenerateMorpherException(); var response = new ServiceErrorMessage(exception); context.Response.StatusCode = (int)exception.ResponseCode; var format = context.Request.Query.Get("format") ?? context.Request.Headers.Get("Accept"); if (format == null) { format = "xml"; } else if (format.Contains("application/json")) { format = "json"; } else if (format.Contains("application/xml")) { format = "xml"; } switch (format) { case "json": context.Response.ContentType = "application/json; charset=utf-8"; context.Response.Write(JsonConvert.SerializeObject(response, Formatting.Indented)); break; case "xml": default: context.Response.ContentType = "application/xml; charset=utf-8"; DataContractSerializer contractSerializer = new DataContractSerializer(typeof(ServiceErrorMessage)); using (MemoryStream memoryStream = new MemoryStream()) { contractSerializer.WriteObject(memoryStream, response); context.Response.Write(memoryStream.ToArray()); } break; } if (!context.Response.Headers.ContainsKey("Error-Code")) { context.Response.Headers.Add("Error-Code", new[] { response.Code.ToString() }); } await Next.Invoke(context); return; } } await Next.Invoke(context); }