public class ResponseOutputFormatter : TextOutputFormatter { public ResponseOutputFormatter(string contentType) { SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(contentType)); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); } public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { var obj = context.Object; var response = context.HttpContext.Response; var settings = new JsonSerializerSettings { DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss", ContractResolver = new CamelCasePropertyNamesContractResolver(), }; // I need know the request whether signed NoOutputFormatAttribute, code like this:
// How can i get filters? if not find NoOutputFormatAttribute means need wrap a custom object if (!filters.Any(a => a is NoOutputFormatAttribute)) { obj = new HttpMessage() { Status = response.StatusCode, Data = obj }; } string json = JsonConvert.SerializeObject(obj, settings); return response.WriteAsync(json); } } action like this(signed with NoOutputFormatAttribute): [HttpGet] [NoOutputFormat] public string Get() { try { throw new IndexOutOfRangeException("Hi, Man... 索引超出范围"); } catch (Exception e) { this.logger.LogError(e, e.StackTrace, new string[] { "附加参数1", "附加参数2" }); } return string.Empty; }
I formatted the http response, wrap a custom object, but sometimes i need skip the ResponseOutputFormatter,
Who can tell me, how could i do?