简体   繁体   中英

asp net core 6.0 kestrel server is not working

I have this code running kestrel

builder.WebHost.UseKestrel().UseUrls("https://myfirstproj1.asp")
.UseIISIntegration();

but I can't access the site through the url I specified. What am I doing wrong?

.net 6 does not use UserUrls() method any more. This is how to do it on .net 6. On Program.cs

var builder = WebApplication.CreateBuilder(args);

//...
builder.WebHost.ConfigureKestrel(options =>
{
    options.ListenAnyIP(5001); // to listen for incoming http connection on port 5001
    options.ListenAnyIP(7001, configure => configure.UseHttps()); // to listen for incoming https connection on port 7001
});
//...
var app = builder.Build();

UseKestrel also works for me. No idea what the difference is:/

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(serverOptions =>
{
    
    serverOptions.ListenAnyIP(4000);
    serverOptions.ListenAnyIP(4001, listenOptions => listenOptions.UseHttps());
});

As of .NET 7 you now need to do this as the above no longer worked for me on .NET 7 projects (although works on .NET 6 projects.

Open appsettings.json and add the following:

{
  //Other code above
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5014"
      }
    }
  }
}

You can specify an endpoint for Https here too but I only needed to set an http port as it is proxied from Apache on a Linux server.

Note that if you add an https option here too and do not have a certificate configured then the site will still preview but will fail to load on the Kestrel server on Ubuntu.

It would appear the above option works on .NET 6 too and it seems simpler as enables the URL and port to be set using the settings file in the same way as other configuration options as opposed to changing the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM