问题
i have the next enum :
public enum DataTypes { [Description("All")] All, Man, Woman }
and i want to achive next :
to display decription for each enum to display the Enum Key and Value.
i used this next post : swagger-ui-web-api-documentation-present-enums-as-strings
I did the next code changes : in the enum, i defined :
[JsonConverter(typeof(StringEnumConverter))] //this is using newton
public enum DataTypes
{
[Description("All")]
All,
[Description("Male222")]
Male,
[Description("Female")]
Female
}
in ConfigureServices added :
services.AddMvc().AddJsonOptions(options =>
options.SerializerSettings.Converters.Add(new StringEnumConverter()));
//and also services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.DescribeAllEnumsAsStrings();
});
in the model that used the Enum i did :
[EnumDataType(typeof(Constans.DataTypes))]
[JsonConverter(typeof(StringEnumConverter))]
public Constans.DataTypes DataType;
the only change i see in swagger that before it showed : [0,1,2] and now : [All,Male,Female] do i miss some settings?
UPDATE : i added above each enum value :
[Description("None enum descriptiondd")]
[EnumMember(Value = "None")]
and replaced to :
c.DescribeAllEnumsAsStrings();
to-> c.DocumentFilter<EnumDocumentFilter>();
for class EnumDocumentFilter i used code from swagger-ui-web-api-documentation-present-enums-as-strings
but i get error :
来源:https://stackoverflow.com/questions/64764537/net-core-2-0-swagger-enum-display