C# .NET5 WEB API Swagger默认加载不显示注释解决方式
第一步:选择项目==》右键==》属性,如下所示:
注:配置文件路径,需配置为程序生成路径
第二步:修改Startup中ConfigureServices,如下所示:
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "MEAS", Version = "v1" }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); //... and tell Swagger to use those XML comments. c.IncludeXmlComments(xmlPath); }); services.AddMvc().AddWebApiConventions();//解决返回值是HttpResponseMessage 自己的 json 序列化数据: //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest).AddJsonOptions(option => //{ // //原样输出,默认会把首字母小写 // option.JsonSerializerOptions.PropertyNamingPolicy = null; //}); }
第三步:重新发布,重启访问 ,如下所示