asp.net-corewebasp.net-core-mvcasp.net-core-webapiwindows-server

ASP.NET Core project works as API but not as website


I'm developing a webapp and I want it to work as navigable website and also as public API as a unique ASP.NET Core app running on a Windows machine.

I have some trouble while trying to run the website and the API together. In my launchSettings.json I understand I have to specify different ports for the website and for the API, I'm in debug mode running in localhost by now.

With this config I understand the listening port on my Windows machine for attending API calls is 7103 and 5182 for http and https connections respectively. This is the part that is working fine right now, my problem comes below.

"ASP.NET_Core_Web_API": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": true,
    "launchUrl": "swagger",
    "applicationUrl": "https://localhost:7103;http://localhost:5182",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  },

And here comes my real problem: With this config I understand the listening port on my Windows machine for attending website connection is 25433, but I get a ERR_CONNECTION_REFUSED while trying to navigate to https://localhost:25433/

"iisSettings": {
  "windowsAuthentication": false,
  "anonymousAuthentication": true,
  "iisExpress": {
    "applicationUrl": "http://localhost:25433",
    "sslPort": 44313
  }
},

ERR_CONNECTION_REFUSED while trying to open https://localhost:25433/

Here is my full launchSettings.json with the specified ports

"$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"
    }
  }
}

And here is my full Main method at Program.cs, maybe I missed something?

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();
}

Solution

  • With this config I understand the listening port on my Windows machine for attending API calls is 7103 and 5182. It is working fine right now (but why 2 different ports are needed?)

    The "ASP.NET_Core_Web_API" inside the lunchsetting.json means this setting is set for the "ASP.NET_Core_Web_API" running directly without using the IIS express.

    The "ASP.NET_Core_Web_API" and "iisSettings" inside the lunchsetting.json means the settings for running the application at two different local side. One is IIS express and another one is Kestrel.

    The reason why 2 different ports are needed is one is for HTTP(http://localhost:5182) and another is for HTTPS(https://localhost:7103). Different schema for http need different port.

    With this config I understand the listening port on my Windows machine for attending website connection is 25433, but I get a ERR_CONNECTION_REFUSED while trying to navigate to https://localhost:25433/

    Why you get the ERR_CONNECTION_REFUSED error, you should check below things.

    The settings is for IIS express, you should make sure you start the application in VS is using IIS express not directly by your project.

    You could check it as below images shows:

    enter image description here

    If you start with the "ASP.NET_Core_Web_API", this means the application is directly running at the kersterl not the IIS express, so it will not use the IIS express settings which will let the http://localhost:25433 not working.