내가 이런 식으로 할 것 :
public class CustomErrorObject
{
public string ErrorCode { get; set; }
public string ErrorDescription { get; set; }
}
public class HandleApiExceptionAttribute : ExceptionFilterAttribute
{
public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
base.OnException(actionExecutedContext);
HttpRequestMessage request = actionExecutedContext.ActionContext.Request;
CustomErrorObject response = new CustomErrorObject();
response.ErrorCode = actionExecutedContext.Exception.Data("Text");
response.ErrorDescription = actionExecutedContext.Exception.Data("Detail");
actionExecutedContext.Response = request.CreateResponse(HttpStatusCode.BadRequest, response);
}
}
을 그 다음의 Global.asax에서 Application_Start 이벤트에이 줄을 추가 :
GlobalConfiguration.Configuration.Filters.Add(new HandleApiExceptionAttribute())
을 사용하면 예외 처리에 대한 자세한 내용을 알고 싶은 경우 웹 API : here