創建一個示例類像下面
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);
}
}
}
在控制器類似下面應該工作
var owincontext = request.GetOwinContext().Environment;
變種headerParser =新HeaderParser(owincontext); headerParser.GetEmployeeNoFromHeader()
我們所做的是我們已經實現了接口IHttpControllerActivator.Create像下面,所以它運行的所有控制器類,是通過依賴注入溫莎城堡產生控制器
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;
}
你有沒有找到解決方案?在控制器中獲取標題的答案不能解決問題。 – 2015-03-06 21:03:16