无法在 launchSettings.JSON 中为 Net.Core 应用程序设置端口 [英] Can't set the port for Net.Core application in launchSettings.JSON
问题描述
我已经编辑了 launchSettings.JSON 文件并像这样更改了端口.
I've edited launchSettings.JSON file and changed the port like so.
"Gdb.Blopp": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "http://localhost:4000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
不过,它仍然在端口 5000 上启动.是该设置被全部忽略了还是我错过了其他东西?
It still starts on port 5000, though. Is that setting disregarded all toghether or am I missing something else?
推荐答案
launchSettings.json
应该由 IDE(即 Visual Studio)使用,当您按 F5/Ctr+F5 并提供开始按钮旁边的下拉菜单中的选项.
The launchSettings.json
supposed to be used by IDEs (i.e. Visual Studio), when you hit F5/Ctr+F5 and offers the options from the pull-down menu next to the start button.
此外,您不应该直接编辑 launcherSettings.json
文件,而是使用项目属性来更改内容.
Also you shouldn't directly edit that the launcherSettings.json
file and instead use the Project Properties to change stuff.
这样做的一个原因是,如果您通过项目属性更改它,Visual Studio 还将编辑 IIS Express 文件(位于解决方案的 .vs/config/applicationhost.config
文件夹中).
One reason for this is that if you change it via project properties, Visual Studio will also edit the IIS Express files (located in the .vs/config/applicationhost.config
folder of your solution).
如果您想更改 kestrel 使用的端口,请使用 .UseUrls("http://0.0.0.0:4000")
(从 appsettings.json
或 hosting.json
) 在 Program.cs
中.
If you want to change the port kestrel uses, use .UseUrls("http://0.0.0.0:4000")
(get it either from appsettings.json
or hosting.json
) in Program.cs
.
如果你不想使用硬编码,你也可以这样做
If you don't want to use hardcoded, you can also do something like this
创建一个hosting.json
:
{
"server": "Microsoft.AspNetCore.Server.Kestrel",
"server.urls": "http://localhost:4000"
}
程序.cs
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: false)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
您也可以通过命令行执行此操作(AddCommandLine
调用在这里很重要,来自 Microsoft.Extensions.Configuration.CommandLine"
包).
You can also do that via commandline (AddCommandLine
call is important here, from Microsoft.Extensions.Configuration.CommandLine"
package).
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
然后通过dotnet run server.urls=http://0.0.0.0:4000
运行.
当您运行 IIS/IISExpress 时,kestrel 端口将由 UseIISIntegration()
确定.
When you run IIS/IISExpress the kestrel port will be determined by UseIISIntegration()
.
这篇关于无法在 launchSettings.JSON 中为 Net.Core 应用程序设置端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!