skip to Main Content

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?

3

Answers


  1. .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();
    
    Login or Signup to reply.
  2. 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());
    });
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search