代码之家  ›  专栏  ›  技术社区  ›  Eric Ouellet

红隼从VS2019开始:页面确定,从可执行文件开始:404

  •  0
  • Eric Ouellet  · 技术社区  · 4 年前

    • 从调试可执行文件:页面未找到错误404。

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

    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
    

    工作正常(从VisualStudio2019开始:调试-任何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();
                });
            }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   ProgrammingLlama Raveena Sarda    4 年前

    您看到的问题与以下代码有关:

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

    当您从VisualStudio运行代码时,它通常会设置 ASPNETCORE_ENVIRONMENT “发展”的环境变量:

    enter image description here

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

    这最终意味着 if (env.IsDevelopment()) 将评估为 false

    解决方案是简单地将您的招摇过市代码移出此块:

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