Create a sample class like below
public class HeaderParser
{
IDictionary<string, object> _requestContext;
IDictionary<string, string[]> _headers;
public HeaderParser(IDictionary<string, object> requestContext)
{
_requestContext = requestContext;
_headers = requestContext["owin.RequestHeaders"] as IDictionary<string, string[]>;
}
public string GetEmployeeNoFromHeader()
{
if (_headers != null && _headers.ContainsKey("X-EmployeeNo") && _headers["X-EmployeeNo"] != null && _headers["X-EmployeeNo"].Length > 0)
{
return _headers["X-EmployeeNo"][0];
}
else
{
var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
response.Content = new StringContent("EMPLOYEE NO NOT AVAILABLE IN REQUEST");
throw new HttpResponseException(response);
}
}
}
In the controller something like below should work
var owincontext = request.GetOwinContext().Environment;
var headerParser= new HeaderParser(owincontext);
headerParser.GetEmployeeNoFromHeader()
What we have done is we have implemented interface IHttpControllerActivator.Create like below, so it runs for all controller class, The controller is generated by dependency injection windsor castle
public IHttpController Create(
HttpRequestMessage request,
HttpControllerDescriptor controllerDescriptor,
Type controllerType)
{
var owincontext = request.GetOwinContext().Environment;
var headerParser= new HeaderParser(owincontext);
var logger = _dpendencyManager.Resolve(typeof(IPOSLogger)) as IPOSLogger;
var executionContext = new ExecutionContext(logger, owincontext,headerParser.GetEmployeeNoFromHeader());
var controller =
(IHttpController)_dpendencyManager.Resolve(controllerType, new { context = executionContext });
//var controller =
// (IHttpController)_dpendencyManager.Resolve(controllerType);
request.RegisterForDispose(
new Release(
() => _dpendencyManager.Release(controller)));
return controller;
}