Web API global error handling add custom header in response
Asked Answered
M

2

6

I was wondering if it was possible to set some custom header values whenever an internal server error has occurred? I am currently doing:

public class FooExceptionHandler : ExceptionHandler
{
    public override void Handle(ExceptionHandlerContext context)
    {
        // context.Result already contains my custom header values
        context.Result = new InternalServerErrorResult(context.Request);
    }
}

Here I also want to set some header values but though it appears in the request the response does not contain it.

Is there a way of doing this?

Maillol answered 27/7, 2015 at 12:26 Comment(0)
R
2

There is a sample code for your reference, my ApiExceptionHandler is your FooExceptionHandler

    public class ApiExceptionHandler : ExceptionHandler
    {
        public override void Handle(ExceptionHandlerContext context)
        {
            var response = new Response<string>
            {
                Code = StatusCode.Exception,
                Message = $@"{context.Exception.Message},{context.Exception.StackTrace}"
            };

            context.Result = new CustomeErrorResult
            {
                Request = context.ExceptionContext.Request,
                Content = JsonConvert.SerializeObject(response),                
            };
        }
    }

    internal class CustomeErrorResult : IHttpActionResult
    {
        public HttpRequestMessage Request { get; set; }

        public string Content { get; set; }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response =
                new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(Content),
                    RequestMessage = Request
                };

            response.Headers.Add("Access-Control-Allow-Origin", "*");
            response.Headers.Add("Access-Control-Allow-Headers", "*");

            return Task.FromResult(response);
        }
    }
Rudiger answered 8/5, 2018 at 10:30 Comment(0)
O
0

It should be possible by creating your own exception filter.

namespace MyApplication.Filters
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http.Filters;

    public class CustomHeadersFilterAttribute : ExceptionFilterAttribute 
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            context.Response.Content.Headers.Add("X-CustomHeader", "whatever...");
        }
    }

}

http://www.asp.net/web-api/overview/error-handling/exception-handling

Organometallic answered 27/7, 2015 at 13:55 Comment(1)
I was hoping this would be possible via the exception handler however, after further investigation and de-compiling I found that the headers get stripped out to its bare bones. I think using the filter is the only way or via a DelegatingHandler.Maillol

© 2022 - 2024 — McMap. All rights reserved.