gpt4 book ai didi

c# - 使用 Kestrel 时可配置的端口号?

转载 作者:行者123 更新时间:2023-11-30 23:20:52 26 4
gpt4 key购买 nike

我已完成以下操作,但仍然无效。运行 dotnet myapp.dll 仍然显示它正在监听 http://localhost:5000 .

  1. 创建hosting.json

代码:

{
"server.url": "http://*:5001"
}
  1. 更新了 Program.cs

代码:

public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
.Build();

var host = new WebHostBuilder()
.UseConfiguration(config) // added
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
//.UseUrls("http://*:5001")
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}
}
  1. 更新了 project.json

代码:

  "publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config",
"NLog.config",
"hosting.json"
]

最佳答案

  1. 您需要更改顺序:.SetBasePath 应该在读取文件之前调用

    var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
    .Build();
  2. 使用server.urls,而不是server.url

关于c# - 使用 Kestrel 时可配置的端口号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39581010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com