如何使用IHttpActionResult设置标头

时间:2017-01-03 15:29:49

标签: asp.net-mvc web-services model-view-controller

我想为webapi请求添加标头到标头。 我怎样才能做到这一点 ?

这是我的示例代码

childDialog.setLocationRelativeTo( parentFrame );
childDialog.setVisible( true );
谢谢。

2 个答案:

答案 0 :(得分:6)

  1. 您可以使用ResponseMessageResult类:

    public IHttpActionResult Authenticate(Login data)
    {
        var Token = "fdsf123546fskjhf-gsuyuhsh";
    
        var response = new HttpResponseMessage();
    
        response.Headers.Add("Authentication", Token);
    
        return new ResponseMessageResult(response);
    }
    
  2. 或者创建自己的结果类:

    public class HeaderActionResult : IHttpActionResult
    {
        private Tuple<string, string> header;
    
        public HeaderActionResult(Tuple<string, string> header) 
        {
            this.header = header;
        }
    
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
        {
            var response = new HttpResponseMessage();
    
            response.Headers.Add(header.Item1, header.Item2);
    
            return Task.FromResult<HttpResponseMessage>(response);
        }
    }
    
    public IHttpActionResult Authenticate(Login data)
    {
        var Token = "fdsf123546fskjhf-gsuyuhsh";
    
        return new HeaderActionResult(Tuple.Create("Authentication", Token));
    }
    
  3. 或使用扩展方法向任何现有的IHttpActionResult对象添加标头:

    public static class HttpActionResultExtensions
    {
        public static IHttpActionResult AddHeader(this IHttpActionResult actionResult, string name, string value)
        {
            return new HeaderActionResult(actionResult, name, value);
        }
    
        private class HeaderActionResult : IHttpActionResult
        {
            private readonly IHttpActionResult actionResult;
    
            private string name;
    
            private string value;
    
            public HeaderActionResult(IHttpActionResult actionResult, string name, string value)
            {
                this.actionResult = actionResult;
    
                this.name = name;
    
                this.value = value;
            }
    
            public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                HttpResponseMessage httpResponseMessage = await this.actionResult.ExecuteAsync(cancellationToken);
    
                httpResponseMessage.Headers.Add(this.name, this.value);
    
                return httpResponseMessage;
            }
        }
    }
    

答案 1 :(得分:0)

这段代码让我工作

    public IHttpActionResult Authenticate(Login data)
            {
                        Status = "success";
                        Message = "You have been Authenticated successfully";
                        var Token = "fdsf123546fskjhf-gsuyuhsh";
                        var Result = new { Status = Status, Message = Message };

                        var Response = Request.CreateResponse(HttpStatusCode.OK, Result);
                        Response.Headers.Add("AccessToken", Token.AccessToken);

                        return ResponseMessage(Response);
            }