コード例 #1
0
        /// <summary>
        /// Called after an action has thrown an <see cref="Exception" />.
        /// </summary>
        /// <param name="context">The <see cref="ExceptionContext" />.</param>
        public void OnException(ExceptionContext context)
        {
            var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            var descriptor       = context.ParseMethodDescriptor(actionDescriptor);

            if (descriptor != null)
            {
                var statusCode = Options.HttpStatusCodeResolver?.Invoke(context.Exception);
                if (statusCode.HasValue)
                {
                    context.HttpContext.Response.StatusCode = (int)statusCode.Value;
                }
                var exceptionDescriptor = Options.ExceptionDescriptorResolver?.Invoke(context.Exception);
                exceptionDescriptor?.PostInitializeWith(actionDescriptor?.MethodInfo.GetCustomAttributes <ExceptionDescriptorAttribute>());
                Options.ExceptionCallback?.Invoke(context.Exception, exceptionDescriptor);
                context.Result = new ExceptionDescriptorResult(exceptionDescriptor);
            }
        }