在控制器构造的Web API读取头值 [英] Web API read header value in controller constructor

查看:423
本文介绍了在控制器构造的Web API读取头值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得在一个web API控制器的构造函数的头信息?我想设置基于掀起了头值的变量,但我不希望有这样做对每个方法。我在自定义头值特别感兴趣,但我会满足于授权逐个这一点。我可以得到它在AuthorizationFilterAttribute工作,但我还需要在控制器级别。

  [PolicyAuthorize]
公共类PoliciesController:ApiController
{
   公共PoliciesController()
   {
      VAR X = HttpContext.Current; //将在构造空
   }   公众的Htt presponseMessage的get()
   {
      VAR X = HttpContext.Current; //将可用,但为时已晚
   }
}
公共类PolicyAuthorizeAttribute:AuthorizationFilterAttribute
{
    公共覆盖无效OnAuthorization(HttpActionContext ActionContext中)
    {
        VAR authHeader = actionContext.Request.Headers.Authorization; //可以在授权头在这里,但在控制器中没有HTTPActionContext
    }
}


解决方案

以下是你可以考虑的一些选项... preFER 1 2


  1. 存放在当前请求消息的属性包的附加数据的Htt prequestMessage.Properties 并在控制器的便捷属性,该属性在控制器中的所有动作都可以访问

      [CustomAuthFilter]
    公共类Values​​Controller:ApiController
    {
        公共字符串名称
        {
            得到
            {
                返回Request.Properties [名称]的ToString()。
            }
        }    公共字符串GETALL()
        {
            返回this.Name;
        }
    }公共类CustomAuthFilter:AuthorizationFilterAttribute
    {
        公共覆盖无效OnAuthorization(HttpActionContext ActionContext中)
        {
            actionContext.Request.Properties [名称] =<从标题&GT你的价值;
        }
    }


  2. 您可以得到当前控制器的实例,并设置属性值。例如:

      [CustomAuthFilter]
    公共类Values​​Controller:ApiController
    {
        公共字符串名称{;组; }    公共字符串GETALL()
        {
            返回this.Name;
        }
    }公共类CustomAuthFilter:AuthorizationFilterAttribute
    {
        公共覆盖无效OnAuthorization(HttpActionContext ActionContext中)
        {
            Values​​Controller values​​Cntlr = actionContext.ControllerContext.Controller为Values​​Controller;        如果(values​​Cntlr!= NULL)
            {
                values​​Cntlr.Name =<从标题&GT你的价值;
            }
        }
    }


Is it possible to get at the header information in the constructor of a web API controller? I want to set variables based off a header value but I don't want to have to do it for each method. I'm particularly interested in a custom header value but I would settle for the Authorization one at this point. I can get it to work in an AuthorizationFilterAttribute but I also need it at the controller level.

[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
    }
}

解决方案

Following are some options that you can consider...prefer 1. over 2.

  1. Store additional data in current request message's properties bag HttpRequestMessage.Properties and have a convenience property in controller which all actions in the controller can access.

    [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>";
        }
    }
    

  2. You could get the current controller's instance and set the property value. Example:

    [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>";
            }
        }
    }
    

这篇关于在控制器构造的Web API读取头值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆