Tas*_*sto 1 .net c# api asp.net-web-api
所有在端口 8888 上公开 .NET 6 Web API 的尝试都会失败。
我通过在 appsettings.json 中添加 url 并在 Program.cs 中配置端口,在线尝试了这些答案和各种其他文章,但没有成功。
更新: Web API 在默认端口上启动,而不是在我想要公开它的端口上启动。在 Program.cs 中配置端口时,我收到
System.InvalidOperationException:“不支持更改 URL,因为地址为只读。”
app.Run("http://localhost:8888"); // This code results in Exception above
Run Code Online (Sandbox Code Playgroud)
或者
System.NotSupportedException:“集合具有固定大小。”
app.Urls.Add("http://localhost:8888"); // The Exception above occurs after running this line of code
Run Code Online (Sandbox Code Playgroud)
运行下面的代码不会更改端口,并且 API 在默认端口上启动
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));
Run Code Online (Sandbox Code Playgroud)
将该行添加builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(PortNumber));
到 Program.cs 文件应该允许您更改 .NET 6 Web API 上的默认端口。
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
Run Code Online (Sandbox Code Playgroud)
当您调试时,上述更改也应该有效,但是如其他答案中所述,在开发中更改此更改的另一种方法是,您可以使用解决方案的属性文件夹下的 launchSettings.json 文件、与此文件相关的文档以及它的使用方式作品可以在这里找到,你想要的一个例子是:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:31312",
"sslPort": 44346
}
},
"profiles": {
"ProfileNameHere": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:8887;http://localhost:8888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件部分将影响开发环境中的 kestrel 端口,因此您应该将其更新为所需的值。您还可以创建具有不同端口和配置的多个配置文件。
归档时间: |
|
查看次数: |
8840 次 |
最近记录: |