【问题标题】:ASP.net swagger API return 200 using curl -I localhost:5100/index.html while returns 404 for localhost:5100?ASP.net swagger API 使用 curl -I localhost:5100/index.html 返回 200,而为 localhost:5100 返回 404?
【发布时间】:2023-04-04 10:17:01
【问题描述】:

当我使用 curl -I localhost:5100 验证它到达主页时,我在 Azure K8s 集群中部署了一个 ASP.net core swagger API,当我尝试 curl -I locolhost:5100/index.html i 时出现错误 404得到响应 200。

知道这是我的启动课程可能导致此问题的原因

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var healthCheckOptions = new HealthCheckOptions
            {
                ResponseWriter = WriteReadinessResponse
            };

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health/readiness", healthCheckOptions);
                endpoints.MapHealthChecks("/health/liveness", new HealthCheckOptions()
                {
                    Predicate = (_) => false
                });
            });

            // enable swagger support
            app.UseSwagger();

            app.UseSwaggerUI(c =>
           {
               c.RoutePrefix = string.Empty;//all request to route [/] are being forward to index.html somehow
               c.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo API V1");
           });
        }

有没有办法解决这个问题?我喜欢 curl -I localhost:5100 时得到响应 200。

【问题讨论】:

    标签: asp.net-core routes swagger


    【解决方案1】:

    DefaultFilesOptions默认重定向,如果url路由没有指定静态文件。

    在您的配置方法中,尝试:

            var defaultFileOptions = new DefaultFilesOptions();
        defaultFileOptions.DefaultFileNames.Clear();
        defaultFileOptions.DefaultFileNames.Add("index.html");
    
        app.UseDefaultFiles(defaultFileOptions);
    

    【讨论】:

    • 运行应用时默认的url是:localhost:5100/swagger/index.html,我用的是c.RoutePrefix = string.Emptylocalhost:5100 路由到 localhost:5100/index.html,但是您的解决方案并不能解决问题
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2017-10-30
    • 2015-04-05
    • 2015-04-26
    • 2015-05-15
    • 2019-10-29
    • 2020-11-22
    • 2018-06-28
    相关资源
    最近更新 更多