NSwag AspNetCore骆驼案例生成设置

时间:2019-06-07 10:04:46

标签: asp.net-core swagger

由于Swagger是最新版本,因此此代码(由于GeneratorSettings)已过时:

 app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
        {
            settings.GeneratorSettings.DefaultPropertyNameHandling =
                PropertyNameHandling.CamelCase;
        });

相反,我们应该使用:

services.AddSwaggerDocument(cfg =>
        {
           ...
        });

但是,我不知道现在在哪里设置骆驼案。有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

添加骆驼包

services.AddSwaggerDocument(settings =>
{
  settings.SerializerSettings = new JsonSerializerSettings
  {
      ContractResolver = new CamelCasePropertyNamesContractResolver()
  };
 });

答案 1 :(得分:0)

在与github上的主要NSwag开发人员进行协商之后,应将NSwag设置与MVC设置一起配置,最终成为解决方案:

services
  .AddMvc()
  .AddNSwagSettings();

扩展方法:

public static IMvcBuilder AddNSwagSettings(this IMvcBuilder builder) =>
  builder.AddJsonOptions(opt =>
    {
      opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });