Asp.Net Core更改launchSettings.json中的url无效

yr9zkbsy  于 2023-04-22  发布在  .NET
关注(0)|答案(4)|浏览(367)

我想改变默认的url(http://localhost:5000)当我运行的网站作为一个控制台应用程序。
我编辑了launchSettings.json,但它不工作...它仍然使用端口5000:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:4230/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "website": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:80",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
vmdwslir

vmdwslir1#

你需要在构建“BuildWebHost”时添加url。希望这对你有帮助https://github.com/aspnet/KestrelHttpServer/issues/639
下面是我在。net核心2.0控制台应用程序中使用的代码

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("http://localhost:5050/")
            .Build();
        
    
}

控制台输出的屏幕截图:

piztneat

piztneat2#

使用Kestrel,您可以使用hosting.json文件指定端口。
将hosting.json添加到您的项目中,并包含以下内容:

{
    "server.urls": "http://0.0.0.0:5002" 
}

并添加到project.json中的publishOptions

"publishOptions": {
"include": [
  "hosting.json"
  ]
}

以及在创建WebHostBuilder时应用程序调用“.UseConfiguration(config)”的入口点中:

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

            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
8nuwlpux

8nuwlpux3#

如果您希望此按钮再次与您想要的端口一起工作

删除项目文件夹中的bin/Debug,并感到高兴

r7s23pms

r7s23pms4#

我遇到了类似的问题,在launchSettings.json中更改applicationUrl无效。
最后发现在appsettings.jsonappsettings.Development.json中被该项覆盖

"urls": "https://localhost:5101",

相关问题