简体   繁体   English

asp.net 核心 6.0 kestrel 服务器不工作

[英]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.但是我无法通过我指定的url访问该站点。 What am I doing wrong?我究竟做错了什么?

.net 6 does not use UserUrls() method any more. .net 6 不再使用 UserUrls() 方法。 This is how to do it on .net 6. On Program.cs这是.net上的做法 6.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. UseKestrel 也适用于我。 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.从 .NET 7 开始,您现在需要执行此操作,因为以上不再适用于 .NET 7 个项目(尽管适用于 .NET 6 个项目。

Open appsettings.json and add the following:打开 appsettings.json 并添加以下内容:

{
  //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.您也可以在这里为 Https 指定一个端点,但我只需要设置一个 http 端口,因为它是从 Linux 服务器上的 Apache 代理的。

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.请注意,如果您在此处也添加了 https 选项并且未配置证书,则该站点仍将预览,但将无法在 Ubuntu 上的 Kestrel 服务器上加载。

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.看起来上面的选项也适用于 .NET 6 并且它看起来更简单,因为可以使用设置文件以与其他配置选项相同的方式设置 URL 和端口,而不是更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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