Search code examples
wcf-web-api

Get the Header Value In wcfWebapi Using Handler


How can i get the Header value in the WcfwebApi using the Handler before entering the Web-method .


Solution

  • You can use an OperationHandler<RequestMessage, RequestMessage>

    It gets passed a RequestMessage in which you can access the the headers:

    public class HeaderAccessRequestHandler : HttpOperationHandler<HttpRequestMessage, HttpRequestMessage> {
    
        public HeaderAccessRequestHandler() : base("request") {
        }
    
        public override HttpRequestMessage OnHandle(HttpRequestMessage request) {
    
            request.Headers.Remove("Accept"); // remove value and 
            request.Headers.Add("Accept","text/html"); // set it new = update 
    
            return request;
        }
    }