簡體   English   中英

Asp.Net Core 在 launchSettings.json 中更改 url 不起作用

[英]Asp.Net Core change url in launchSettings.json not working

當我將網站作為控制台應用程序運行時,我想更改默認值 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"
      }
    }
  }
}

使用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();
        }

這是一個已知的問題(我不能指出GitHub上的一個問題,因為它是一個私人回購)。

您需要在構建“BuildWebHost”時添加URL。 希望這個可以幫助你https://github.com/aspnet/KestrelHttpServer/issues/639

下面是我在.net core 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();


}

控制台輸出的屏幕截圖

如果你想讓這個按鈕再次與你想要的端口一起工作

在此處輸入圖像描述

刪除項目文件夾中的bin/Debug並開心

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM