Home > front end >  Kestrel starting from VS2019 : page ok, starting from executable : 404
Kestrel starting from VS2019 : page ok, starting from executable : 404

Time:11-17

Just created asp.Net core project.

  • From VS2019 : I have the requested page.
  • From the debug executable : Page not found error 404.

using : http://localhost:5555/swagger/index.html on both.

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

Does not work (executable direct)

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

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

Does work fine (Started from Visual Studio 2019 : Debug - Any 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"
}

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

I have no appsettings.Production.json

appsettings.Development.json:

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

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

ConfigureServices and Configure code:

// 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();
            });
        }

CodePudding user response:

The issue you're seeing is down to this code:

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

When you run your code from Visual Studio, it normally sets the enter image description here

This in turn sets the environment in code to "Development" when the application starts. If this value isn't set, it will default to "Production"

Ultimately this means that if (env.IsDevelopment()) will evaluate to false, as it isn't the development environment anymore, and the code within the block won't run.

The solution is to simply move your Swagger code outside this block:

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

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