无法在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?
推荐答案
当您按下F5/Ctr + F5并提供下拉菜单中的选项时,IDE(即Visual Studio)应该使用的launchSettings.json
开始按钮旁边的菜单.
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).
如果要更改红est使用的端口,请使用Program.cs
中的.UseUrls("http://0.0.0.0:4000")
(从appsettings.json
或hosting.json
获取).
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"
}
Program.cs
Program.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();
}
}
您也可以通过命令行进行操作(在Microsoft.Extensions.Configuration.CommandLine"
包中,AddCommandLine
调用在这里很重要).
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时,茶est端口将由UseIISIntegration()
确定.
When you run IIS/IISExpress the kestrel port will be determined by UseIISIntegration()
.
这篇关于无法在launchSettings.JSON中设置Net.Core应用程序的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!