Home > Mobile >  Any way to get request headers in IDocumentFilter class? (Swashbuckle)
Any way to get request headers in IDocumentFilter class? (Swashbuckle)

Time:07-19

Here's my definition for the IDocumentFilter class -

public class ShowDocumentationFilter : IDocumentFilter
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
    public class ShowDocumentationAttribute : Attribute
    {
    }

    void IDocumentFilter.Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        //if request does not contain header, do this:
        foreach (ApiDescription apiDescription in apiExplorer.ApiDescriptions)
        {
            if ((apiDescription.RelativePathSansQueryString().StartsWith("api/System/"))
                || (apiDescription.RelativePath.StartsWith("api/Internal/"))
                || (apiDescription.Route.RouteTemplate.StartsWith("api/OtherStuff/"))
                )
            {
                swaggerDoc.paths.Remove("/"   apiDescription.Route.RouteTemplate.TrimEnd('/'));
            }
        }
    }
}

I'd like to apply this my endpoints, but is there a way to check in this class whether requests hitting my endpoints have a certain header?

CodePudding user response:

You can access HttpContext inside Apply method. you can check for any required header.

enter image description here

  • Related