2017-07-31 91 views
3

这是我Program.cs的.Net核心API起始URL

public static void Main(string[] args) 
{ 
    var host = new WebHostBuilder() 
     .UseKestrel() 
     .UseContentRoot(Directory.GetCurrentDirectory()) 
     .UseUrls("http://localhost:9020") 
     .UseIISIntegration() 
     .UseStartup<Startup>() 
     //.UseApplicationInsights() 
     .Build(); 

    host.Run(); 
} 

它用来在端口运行9020作为UseUrls()下指定。出于某种原因,现在当我启动该程序时,它会担任端口54033和唯一的事情(我觉得)我已经改变是增加:

<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers> 

.csproj发布,但我没有看到这将如何影响运行服务的端口。有其他地方我可以检查这个问题吗?

回答

4

在您的项目的属性下,您可以找到launchSettings.json。在这里,您可以定义应用程序可以启动的配置文件。 ApplicationUrl为应用程序设置设置网址。

enter image description here

例JSON:

"profiles": { 
"IIS Express": { 
    "commandName": "IISExpress", 
    "launchBrowser": true, 
    "launchUrl": "api", 
    "environmentVariables": { 
    "ASPNETCORE_ENVIRONMENT": "Development" 
    } 
}, 
"RunInCommandPrompt": { 
    "commandName": "Project", 
    "launchBrowser": true, 
    "launchUrl": "api", 
    "environmentVariables": { 
    "ASPNETCORE_ENVIRONMENT": "Development" 
    }, 
    "applicationUrl": "http://localhost:19556" 
} 
} 
+0

这工作......但现在我不知道为什么不是'Program.cs'选项覆盖它?在两个地方改变它有什么意义? – Norgul

+0

我不建议在代码中使用硬编码的网址,通过设置来设置它更简洁。 Program.cs中的代码被覆盖的原因是因为您是从Visual Studio启动应用程序,并将其配置存储在launchSettings.json文件中。 –