Swashbuckle.aspnetcore: add headers to a response

Created on 4 Oct 2017  路  5Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I鈥檓 wondering if it鈥檚 possible to add customized headers to a response, for the support of pagination in RESTful APIs.

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

```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 });
                        }
                    });

            }
        }

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoelAdamWeiss picture JoelAdamWeiss  路  4Comments

engelhardtda picture engelhardtda  路  3Comments

brucewilkins picture brucewilkins  路  3Comments

m-demydiuk picture m-demydiuk  路  3Comments

jderus picture jderus  路  4Comments