如何使用IHttpActionResult时设置自定义页眉? [英] How to set custom headers when using IHttpActionResult?

查看:319
本文介绍了如何使用IHttpActionResult时设置自定义页眉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的ASP.NET Web API 2, IHttpActionResult 在简化控制器code提供了很大的价值,我不愿意停止使用它,但我'已经打了一个问题。

In ASP.NET Web API 2, the IHttpActionResult offers a lot of value in simplifying controller code and I'm reluctant to stop using it, but I've hit a problem.

我需要设置一个ETag上传出的反应,我找不到,让我获得了响应的头部任何财产。目前我使用的是确定&LT; T&GT;(T含量) ApiController 的辅助方法,它返回一个<一个href=\"http://msdn.microsoft.com/en-us/library/dn308866%28v=vs.118%29.aspx\"><$c$c>OkNegotiatedContentResult<T>目的。这似乎并没有有任何暴露的这会让我虽然修改标题。

I need to set an ETag on an outgoing response, and I cannot find any property which gives me access to the response's headers. At the moment I'm using the Ok<T>(T content) helper method from the ApiController, which returns an OkNegotiatedContentResult<T> object. That doesn't seem to have anything exposed which would let me modify the headers though.

我缺少的东西,或者是真的没有办法同时使用存量 IHttpActionResult 类型来做到这一点?我认为是一个消息处理程序,但后来我不得不弄清楚如何ETag的传递出的动作(在ETag是针对不同的行动产生不同的,所以它不是做一个通用的处理程序,所有行动的问题)。

Am I missing something, or is there really no way to do this while using the stock IHttpActionResult types? I considered a message handler, but then I'd have to figure out how to pass the ETag out of the action (the ETags are generated differently for different actions, so it's not a matter of making a generic handler for all actions).

我想避免使用原始的Htt presponseMessage,但在时刻要寻找困难。

I'd like to avoid having to use the raw HttpResponseMessage, but at the moment that's looking difficult.

推荐答案

有关您的情况,您需要创建一个自定义 IHttpActionResult 。以下是我从派生一个例子 OkNegotiatedContentResult&LT; T&GT; 因为它运行内容协商并设置确定状态code。

For your scenario, you would need to create a custom IHttpActionResult. Following is an example where I derive from OkNegotiatedContentResult<T> as it runs Content-Negotiation and sets the Ok status code.

public class CustomOkResult<T> : OkNegotiatedContentResult<T>
{
    public CustomOkResult(T content, ApiController controller)
        : base(content, controller) { }

    public CustomOkResult(T content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters) 
        : base(content, contentNegotiator, request, formatters) { }

    public string ETagValue { get; set; }

    public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        HttpResponseMessage response = await base.ExecuteAsync(cancellationToken);

        response.Headers.ETag = new EntityTagHeaderValue(this.ETagValue);

        return response;
    }        
}

控制器

public class ValuesController : ApiController
{
    public IHttpActionResult Get()
    {
        return new CustomOkResult<string>(content: "Hello World!", controller: this)
            {
                    ETagValue = "You ETag value"
            };
    }
}

请注意,您也可以从 NegotiatedContentResult&LT获得; T&GT; ,在这种情况下,您需要提供状态code自己。希望这有助于。

Note that you can also derive from NegotiatedContentResult<T>, in which case you would need to supply the StatusCode yourself. Hope this helps.

您可以找到的 <一个源$ C ​​$ C href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Http/Results/OkNegotiatedContentResult.cs\"><$c$c>OkNegotiatedContentResult<T> <$c$c>NegotiatedContentResult<T>,正如你可以想像是简单的实际。

You can find the source code of OkNegotiatedContentResult<T> and NegotiatedContentResult<T>, which as you can imagine are simple actually.

这篇关于如何使用IHttpActionResult时设置自定义页眉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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