I鈥檓 wondering if it鈥檚 possible to add customized headers to a response, for the support of pagination in RESTful APIs.
The Response class has a Headers property but as far as I can see it's never set anywhere in the Swashbuckle code. You can set it yourself though, probably using an OperationFilter is the best approach.
@SimonTouchtech Thanks! It works!
@will-beta - care to share a code sample for the benefit of others?
Thanks
@freeranger
```c#
///
/// custom attribute for API
///
class ResponseHeaderAttribute : Attribute
{
public string HttpCode { get; set; }
public string Name { get; set; }
public Type Type { get; set; }
public object Default { get; set; }
public string Description { get; set; }
}
```c#
class ResponseHeaderOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
context?.ApiDescription?.ActionAttributes()
?.OfType<ResponseHeaderAttribute>()
?.ToList()
?.ForEach(a =>
{
var response = operation.Responses[a.HttpCode];
if (response != null)
{
response.Headers = response.Headers ?? new System.Collections.Generic.Dictionary<string, Header>();
response.Headers.Add(a.Name, new Header { Type = a.Type.Name, Default = a.Default, Description = a.Description });
}
});
}
}
Thanks @will-beta
Most helpful comment
@freeranger
```c#
///
/// custom attribute for API
///
class ResponseHeaderAttribute : Attribute
{
public string HttpCode { get; set; }
public string Name { get; set; }
public Type Type { get; set; }
public object Default { get; set; }
public string Description { get; set; }
}