【问题标题】:How can I get my API endpoints to show in swagger? C# ASP .NET如何让我的 API 端点大张旗鼓地显示? C# ASP .NET
【发布时间】:2022-08-18 21:21:08
【问题描述】:

在我的应用程序中,我创建了一些最小端点。我可以通过邮递员等工具调用它们,但它们并没有大张旗鼓地显示。

就我而言,该应用程序使用带有启动类的旧样式,因此我从那里调用以注册我的端点。

这是我如何创建它们的示例。

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet(\"/testEndpoint\", () =>
                {
                    return \"Test\";
                });
            }

在我看到的创建最小 API 的所有示例中,我看到您可以直接从 \"app\" 和 IApplicationBuilder 调用 \"MapGet\"。在我的情况下,我需要调用“app.UseEndpoints”。

另一件要提的是,应用程序中有控制器,并且所有这些端点都被显示出来。

我不确定是否有办法解决这个问题,所以我可以让他们大摇大摆地出现。

    标签: c# asp.net


    【解决方案1】:
    public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers()
                    .AddNewtonsoftJson(opt => opt.SerializerSettings.Converters.Add(new StringEnumConverter()));
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "OwsApi", Version = "v1" });
                    c.ExampleFilters();
                    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    c.IncludeXmlComments(xmlPath);
                    c.EnableAnnotations();
                }).AddSwaggerGenNewtonsoftSupport();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
    #if DEBUG
                app.UseDeveloperExceptionPage();
    #endif
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "OwsApi v1"));
                app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多