Search code examples
swagger-uiswashbuckle

Swagger Response Attributes for Controller Action Errors


I am struggling to find some examples of the SwaggerResponse attribute using the controller action errors in my C# web api, such as NotFound() etc.

I have this attribute on my controller method:

[SwaggerResponse(404, "Requested application not found", Type = typeof(NotFoundResult))]

And in the controller method I have this:

NotFound("No configuration found")

But in the UI I get this:

404

I was hoping it would at least show the code in the json and not 0?

The only thing that looks reasonable in the UI is the 200 response, which is a List<settings> which shows the list, but doesn't show examples.


Solution

  • After finally getting the search terms correct for this I found an answer on here that actually suppresses the error models all together, which is better than having the above in your documentation.

    Using net5.0 this worked for me:

    // add controllers with swagger
    services.AddControllers(x => x.Conventions.Add(new ApiExplorerVersionConvention()))
        .ConfigureApiBehaviorOptions(x => { x.SuppressMapClientErrors = true; });
    

    For more info for older .Net versions see here:

    Hide Swagger Bad Response for .net 2.2