当WebAPI
中发生意外错误时,用户会看到整个堆栈跟踪。
我认为显示整个堆栈跟踪并不安全。
停止向我的用户显示整个跟踪的默认行为是什么?
仅仅说出Internal Server Error
这样的友好信息就足够了。正确的吗?
任何想法如何?
<?xml version="1.0"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>The method or operation is not implemented.</ExceptionMessage>
<ExceptionType>System.NotImplementedException</ExceptionType>
<StackTrace> at MyCompany.BLL.RequirementOfService.Employee1.Employee1Service.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.BAL.RequirementOfService\Employee1\Employee1Service.cs:line 37
at MyCompany.BLL.RequirementOfService.RequirementOfServiceBLL.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.BAL.RequirementOfService\RequirementOfServiceBLL.cs:line 76
at MyCompany.RequirementOfService.Windsor.RequirementOfServiceProvider.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.RequirementOfService\Windsor\RequirementOfServiceProvider.cs:line 47
at MyCompany.RequirementOfService.RequirementOfService.Controllers.RequirementOfServiceController.Post(RequirementOfServiceDTO RequirementOfServiceDTO) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.RequirementOfService\RequirementOfService\Controllers\RequirementOfServiceController.cs:line 87
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
答案 0 :(得分:44)
只需将配置IncludeErrorDetailPolicy
更改为LocalOnly
,详细信息就不会发送给客户。
此处:http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api
答案 1 :(得分:3)
对于那些只希望抑制StackTrace而不丢弃重要错误提示的人,可以实现ExceptionFilter。
您可以分两个步骤进行操作:
编写过滤器,如下所示:
using System.Web.Http.Filters;
using System.Net;
using System.Net.Http;
public class MyExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var request = context.Request;
var response = request.CreateErrorResponse(HttpStatusCode.InternalServerError, context.Exception.Message);
var content = (System.Net.Http.ObjectContent<System.Web.Http.HttpError>)response.Content;
var errorValues = (System.Web.Http.HttpError)content.Value;
errorValues["ExceptionMessage"] = context.Exception.Message;
errorValues["ExceptionType"] = context.Exception.GetType().Name;
if (context.ActionContext != null)
{
errorValues["ActionName"] = context.ActionContext.ActionDescriptor.ActionName;
errorValues["ControllerName"] = context.ActionContext.ControllerContext.ControllerDescriptor.ControllerName;
}
context.Response = response;
}
}
使WebApi使用您的ExceptionFilter:
public static void Register(HttpConfiguration config)
{
config.Filters.Add(new MyExceptionFilterAttribute());
您将获得:
{
"Message": "Your exception is here!",
"ExceptionMessage": "Your exception is here!",
"ExceptionType": "Exception",
"ActionName": "MyAction",
"ControllerName": "MyController"
}
更多信息,请访问:https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling