Asp.net

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

  • April 5, 2018

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

建構“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();


}

控制台輸出的螢幕截圖

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

引用自:https://stackoverflow.com/questions/38314636