如何为OData控制器实现敏捷

时间:2018-10-01 09:49:14

标签: asp.net-core .net-core swagger-2.0 aspnetboilerplate

我在asp.net核心应用程序中使用odata控制器。现在用于文档说明,我想使用招摇。我尝试了很多方法,但没有任何效果。

请提出整合摇摇欲坠的方法

1 个答案:

答案 0 :(得分:0)

您需要安装the API Explorer for Microsoft ASP.NET Core and OData v4.0,然后看一看一些samplesdocumentation

文档中的代码段:

public void ConfigureServices( IServiceCollection services )
{
  // format the version as "'v'major[.minor][-status]"
  services.AddMvc();
  services.AddVersionedApiExplorer( o => o.GroupNameFormat = "'v'VVV" );
  services.AddApiVersioning();
  services.AddOData().EnableApiVersioning();
  services.AddSwaggerGen(
    options =>
    {
      var provider = services.BuildServiceProvider()
                             .GetRequiredService<IApiVersionDescriptionProvider>();

      foreach ( var description in provider.ApiVersionDescriptions )
      {
        options.SwaggerDoc(
          description.GroupName,
          new Info()
          {
            Title = $"Sample API {description.ApiVersion}",
            Version = description.ApiVersion.ToString()
          } );
      }
   } );
}

public void Configure(
    IApplicationBuilder app,
    VersionedODataModelBuilder modelBuilder,
    IApiVersionDescriptionProvider provider )
{
    var models = modelBuilder.GetEdmModels();
    app.UseMvc( routes => routes.MapVersionedODataRoutes( "odata", null, models ) );
    app.UseSwagger();
    app.UseSwaggerUI(
        options =>
        {
            foreach ( var description in provider.ApiVersionDescriptions )
            {
                options.SwaggerEndpoint(
                    $"/swagger/{description.GroupName}/swagger.json",
                    description.GroupName.ToUpperInvariant() );
            }
        } );
}