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.
public static class HttpExtentions
{
public static IHttpActionResult AddHeader(this IHttpActionResult action,
string headerName, IEnumerable headerValues)
{
return new HeaderActionResult(action, headerName, headerValues);
}
public static IHttpActionResult AddHeader(this IHttpActionResult action,
string headerName, string header)
{
return AddHeader(action, headerName, new[] {header});
}
private class HeaderActionResult : IHttpActionResult
{
private readonly IHttpActionResult action;
private readonly Tuple> header;
public HeaderActionResult(IHttpActionResult action, string headerName,
IEnumerable headerValues)
{
this.action = action;
header = Tuple.Create(headerName, headerValues);
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
var response = await action.ExecuteAsync(cancellationToken);
response.Headers.Add(header.Item1, header.Item2);
return response;
}
}
}