首页 > 解决方案 > Kestrel 从 VS2019 开始:页面正常,从可执行文件开始: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

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

应用设置.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();
            });
        }

标签: c#asp.net-corehttp-status-code-404appsettingskestrel

解决方案


您看到的问题归结为以下代码:

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

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

在此处输入图像描述

当应用程序启动时,这反过来将代码中的环境设置为“开发”。如果未设置此值,则默认为“生产”

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

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

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

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

推荐阅读