无法在 launchSettings.JSON 中为 Net.Core 应用程序设置端口

编程入门 行业动态 更新时间:2024-10-28 20:27:14
本文介绍了无法在 launchSettings.JSON 中为 Net.Core 应用程序设置端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经编辑了 launchSettings.JSON 文件并像这样更改了端口.

I've edited launchSettings.JSON file and changed the port like so.

"Gdb.Blopp": { "commandName": "Project", "launchBrowser": false, "launchUrl": "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("0.0.0.0:4000")(从 appsettings.json 或 hosting.json) 在 Program.cs 中.

If you want to change the port kestrel uses, use .UseUrls("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": "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=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 应用程序设置端口

本文发布于:2023-11-09 09:05:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   中为   应用程序   launchSettings   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!