[PolicyAuthorize]public class PolicIEsController : APIController{ public PolicIEsController() { var x = httpContext.Current; //will be null in constructor } public httpResponseMessage Get() { var x = httpContext.Current; //will be available but too late }}public class PolicyAuthorizeAttribute : AuthorizationFilterattribute{ public overrIDe voID OnAuthorization(httpActionContext actionContext) { var authheader = actionContext.Request.headers.Authorization; //can get at Authorization header here but no httpActionContext in controller }}解决方法 以下是您可以考虑的一些选项…更喜欢1.超过2.
>将其他数据存储在当前请求消息的属性包httpRequestMessage.PropertIEs中,并在控制器中具有一个便利属性,控制器中的所有 *** 作都可以访问该属性.
[CustomAuthFilter]public class ValuesController : APIController{ public string name { get { return Request.PropertIEs["name"].ToString(); } } public string GetAll() { return this.name; }}public class CustomAuthFilter : AuthorizationFilterattribute{ public overrIDe voID OnAuthorization(httpActionContext actionContext) { actionContext.Request.PropertIEs["name"] = "<your value from header>"; }}
>您可以获取当前控制器的实例并设置属性值.例:
[CustomAuthFilter]public class ValuesController : APIController{ public string name { get; set; } public string GetAll() { return this.name; }}public class CustomAuthFilter : AuthorizationFilterattribute{ public overrIDe voID OnAuthorization(httpActionContext actionContext) { ValuesController valuesCntlr = actionContext.ControllerContext.Controller as ValuesController; if (valuesCntlr != null) { valuesCntlr.name = "<your value from header>"; } }}总结
以上是内存溢出为你收集整理的c# – 控制器构造函数中的Web API读取头值全部内容,希望文章能够帮你解决c# – 控制器构造函数中的Web API读取头值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)