【发布时间】:2015-06-03 07:40:26
【问题描述】:
我有一个与之通信的 web api。
发生异常时,我得到以下 JSON 模板:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Index was outside the bounds of the array.",
"ExceptionType": "System.IndexOutOfRangeException",
"StackTrace": " at WebApiTest.TestController.Post(Uri uri) in c:\\Temp\\WebApiTest\\WebApiTest\\TestController.cs:line 18\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClassf.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"
}
我希望 JSON 包含的只是“Message”和“ExceptionMessage”属性,但仍然可以控制按需返回完整的堆栈跟踪。
我尝试过使用
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy
但似乎全有或全无,要么只是单个“消息”属性,要么在将其设置为“始终”时获取完整对象。
有什么简单的方法可以实现吗?
我们将不胜感激。
【问题讨论】:
标签: c# asp.net json asp.net-web-api