Kestrel 从 VS2019 开始:页面正常,从可执行文件开始:404

Kestrel starting from VS2019 : page ok, starting from executable : 404

刚刚创建了 asp.Net 核心项目。

两者均使用:http://localhost:5555/swagger/index.html。

=========================================== ====

不起作用(可直接执行)

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://0.0.0.0:5555
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Prj\Personnel\WallyRest\WallyRest\bin\Debug\net5.0

=========================================== ====

工作正常(从 Visual Studio 2019 年开始:调试 - 任何 CPU)

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'http://localhost:5115'. Binding to endpoints defined in UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://0.0.0.0:5555
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Prj\Personnel\WallyREst\WallyRest

=========================================== ====

Appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },

  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://0.0.0.0:5555" // ";http://pd140356:8181;http://*:6666",
//      "Url": "http://localhost:5555"
      }
    }
  },

  "AllowedHosts": "*",

  // "urls": "http://*:5115;http://*:8888"
}

=========================================== ====

我没有appsettings.Production.json

appsettings.Development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

=========================================== ====

配置服务和配置代码:

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WallyRest", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

您遇到的问题是由以下代码引起的:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));
}

当您 运行 来自 Visual Studio 的代码时,它通常会将 ASPNETCORE_ENVIRONMENT 环境变量设置为“开发”:

这又会在应用程序启动时将代码中的环境设置为“开发”。如果未设置此值,它将默认为“Production”

最终这意味着 if (env.IsDevelopment()) 将计算为 false,因为它不再是开发环境,块中的代码不会 运行。

解决方案是简单地将您的 Swagger 代码移到此块之外:

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

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));