6

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?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • 2
    What error do you get in the browser? Does your computer know how to DNS resolve your domain name? – gunr2171 Oct 11 '21 at 22:02
  • 2
    `UseUrls()` allows you to specify which protocols, **IP addresses** and ports to listen to. You can't use it to create a custom domain name for your site (to do that you need DNS). As far as I can tell, passing `https://myfirstproj1.asp` is essentially equivalent to `https://*` which listens to **any** IP address. A normal use case would be `https://localhost` or `https://*`. For more information see: [5 ways to set the URLs for an ASP.NET Core app](https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/) – Visual Vincent Oct 11 '21 at 22:14
  • 1
    `UseUrls` won't make your DNS recognize `myfirstproj1.asp` and forward it to your IP. During development you can add `myfirstproj1.asp` to your `hosts` file and point it to the server you want, perhaps even 127.0.0.1. In production you'll have to register that DNS domain and point it to your server(s) – Panagiotis Kanavos Oct 12 '21 at 08:22
  • What are you trying to do though? `myfirstproj1.asp` is a *very* strange domain. Are you trying to intercept calls to a WebForms page perhaps? – Panagiotis Kanavos Oct 12 '21 at 08:22

3 Answers3

25

.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();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nahuel Taibo
  • 459
  • 6
  • 3
1

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());
});
Squibly
  • 335
  • 2
  • 7
0

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.

Robin Wilson
  • 308
  • 2
  • 11