Home > other >  .net 6 use http on localhost api
.net 6 use http on localhost api

Time:01-24

I'm trying to run my webapi under http on localhost so in the program.cs, I have

if (!app.Environment.IsDevelopment())
{
    app.UseHttpsRedirection();
}
else
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

but when I run the api, it still redirects to https and if I try to use http it says localhost cannot load any data. I'm also definitely in development as the swagger ui is displayed.

Is there something else I need to do to run the api under http?

Appsettings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "DbConnection": "From user secrets"
  },
  "AllowedHosts": "*"
}

CodePudding user response:

Add the listening end points in manually.

.UseKestrel(options =>
 {
   // HTTP 8088
   options.ListenLocalhost(8088);
 }

also, .UseUrls("http://*:8088") may work

CodePudding user response:

Turns out it was my launchSettings.json:

"API": {
  "commandName": "Project",
  "dotnetRunMessages": false,
  "launchBrowser": true,
  "launchUrl": "swagger",
  "applicationUrl": "http://localhost:7188",  <-- this line was set to https, changing it to http fixed the issue
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}
  • Related