我正在开发一个 Web 应用程序,我希望它既可以作为可导航的网站,也可以作为公共 API 作为在 Windows 计算机上运行的独特 ASP.NET Core 应用程序。
我在尝试同时运行网站和 API 时遇到了一些麻烦。在我的 launchSettings.json 中,我知道我必须为网站和 API 指定不同的端口,我现在处于在 localhost 中运行的调试模式。
通过此配置,我了解 Windows 计算机上用于参加 API 调用的监听端口是 7103 和 5182。它现在工作正常(但为什么需要 2 个不同的端口?)
"ASP.NET_Core_Web_API": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7103;http://localhost:5182",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
我真正的问题来了:通过这个配置,我知道我的 Windows 机器上用于参加网站连接的监听端口是 25433,但在尝试导航到 https://localhost:25433/ 时我收到 ERR_CONNECTION_REFUSED
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:25433",
"sslPort": 44313
}
},
这是我的完整 launchSettings.json 以及指定的端口
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:25433",
"sslPort": 44313
}
},
"profiles": {
"ASP.NET_Core_Web_API": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7103;http://localhost:5182",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
这是我在 Program.cs 中的完整 Main 方法,也许我错过了一些东西?
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapDefaultControllerRoute();
app.MapControllers();
app.Run();
}
HTTP 和 HTTPS 方案需要 2 个端口。如果您只有一个应用程序,则 URL 取决于启动期间选择的配置(IIS Express 或 ASP.NET_Core_Web_API)。因此,您可以选择配置文件,但不能同时运行它。