Пример #1
0
        public static void HandleProblemDetailsException(this ExceptionContext context, Exception exception)
        {
            var ex = HttpProblemDetailException.FromException(exception);

            if (ex == null)
            {
                return;
            }

            //context.HttpContext.Response.ContentType = GetContentTypeStringForContext(context.HttpContext);
            //context.HttpContext.Response.StatusCode = ex.ProblemDetail.Status;
            context.Result = new ObjectResult(ex.ProblemDetail)
            {
                StatusCode = ex.ProblemDetail.Status,
                //ContentTypes = new MediaTypeCollection { new MediaTypeHeaderValue(GetContentTypeStringForContext(context.HttpContext)) },
                DeclaredType = typeof(IHttpProblemDetail)
            };
        }
Пример #2
0
        public static void HandleProblemDetailsException(this HttpContext context, Exception exception)
        {
            var ex = HttpProblemDetailException.FromException(exception);

            if (ex == null)
            {
                return;
            }

            var json = JsonConvert.SerializeObject(ex.ProblemDetail);

            context.Response.ContentType = GetContentTypeStringForContext(context);
            context.Response.StatusCode  = ex.ProblemDetail.Status;
            var data = Encoding.UTF8.GetBytes(json);

            context.Response.ContentLength = data.Length;
            context.Response.Body.WriteAsync(data, 0, data.Length)
            .Wait();
        }
Пример #3
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var ex = HttpProblemDetailException.FromException(actionExecutedContext.Exception);

            if (ex == null)
            {
                return;
            }

            // get formatter from content negotiator
            var configuration     = actionExecutedContext.ActionContext.ControllerContext.Configuration;
            var contentNegotiator = configuration.Services.GetContentNegotiator();
            var connegResult      = contentNegotiator.Negotiate(typeof(IHttpProblemDetail), actionExecutedContext.Request, configuration.Formatters);
            var formatter         = connegResult.Formatter;

            // return object content
            actionExecutedContext.Response = new HttpResponseMessage((HttpStatusCode)ex.ProblemDetail.Status)
            {
                Content = new ObjectContent(typeof(IHttpProblemDetail), ex.ProblemDetail, formatter, GetContentTypeStringForContext(actionExecutedContext))
            };
        }
        /// <summary>
        /// Enable HttpProblemDetails support in the application
        /// </summary>
        /// <param name="pipelines">Application pipeline to hook into</param>
        /// <param name="responseNegotiator">An <see cref="IResponseNegotiator"/> instance.</param>
        public static void Enable(IPipelines pipelines, IResponseNegotiator responseNegotiator)
        {
            var httpProblemDetailsEnabled = pipelines.AfterRequest.PipelineItems.Any(ctx => ctx.Name == nameof(HttpProblemDetails));

            if (!httpProblemDetailsEnabled)
            {
                pipelines.OnError.AddItemToEndOfPipeline((context, exception) =>
                {
                    var ex = HttpProblemDetailException.FromException(exception);
                    if (ex == null)
                    {
                        return(context.Response);
                    }

                    var negotiator = new Negotiator(context)
                                     .WithContentType(GetContentTypeForContext(context))
                                     .WithStatusCode(ex?.ProblemDetail?.Status ?? 0)
                                     .WithModel(ex.ProblemDetail);

                    return(responseNegotiator.NegotiateResponse(negotiator, context));
                });
            }
        }