To add examples to API responses, implement IExamplesProvider<T> where T is the return type.
Automatic Annotation: Works if the action return type is clearly defined (e.g., ActionResult<T> or T).
Manual Annotation: Use the [SwaggerResponseExample(statusCode, typeof(ExampleClass))] attribute to explicitly link a provider to a specific HTTP status code. This overrides automatic detection.
// Implementation
public class CountryExamples : IExamplesProvider<List<Country>>
{
public List<Country> GetExamples()
{
return new List<Country> { new Country { Code = "AA", Name = "Test" } };
}
}
// Usage
[SwaggerResponse(200, "The list of countries", typeof(IEnumerable<Country>))]
[SwaggerResponseExample(200, typeof(CountryExamples))]
public async Task<HttpResponseMessage> Get(string lang)