且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IIS Express 上的 ASP.NET Core 3.1 应用程序:“无法访问此站点"

更新时间:2023-02-15 21:14:12

从 aspnetcore 2.2 升级到 3.1 时,我的 Program.cs 中似乎缺少某些内容.

直到现在我有这个:

return WebHost.CreateDefaultBuilder(args).ConfigureServices(services => services.AddAutofac()).UseStartup();

在查看一个新的 v3.1 项目时,它有类似的东西:

public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup();});

I'm working on an ASP.NET Core application that I have upgraded from v2.2 to v3.1.

When I start the application using IIS Express without SSL enabled, I get this error in Google Chrome:

This site can't be reached. localhost refused to connect.

When I enable SSL, I get this error instead:

This localhost page can't be found. No web page was found for the web address: https://localhost:44367.

I have have an IIS website configured to the directory where my application sits. In the project settings, I can set the 'Launch' setting to 'IIS', but when I do this I also get the same error.

Screenshot of my web project settings: .

When I create a new blank ASP.NET Core 3.1 web project and run it with the same settings on IIS Express, it runs fine.

Based on suggestions elsewhere, I have tried the following:

  • Cleaning and rebuilding
  • Deleting the .vs folder in the web project
  • Changing the port number (currently it is localhost:54236).
  • Enabling/disabling SSL
  • Restarting my machine
  • Loading up a fresh copy of my solution from Git on a separate working directory and trying to run it.

I'm at a loss.

Here is my launchSettings.json file:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "http://myapplication.debug",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:54236",
      "sslPort": 44367
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

When comparing with the launchSettings.json file of a fresh project I can't see any notable difference. Is there anything else that would cause this failure?

I should note that I did change the port number - it was initially at port number 50192, but when I attempt to run the website to that address (

It appears I was missing something in my Program.cs when upgrading from aspnetcore 2.2 to 3.1.

Until now I had this:

return WebHost.CreateDefaultBuilder(args)
       .ConfigureServices(services => services.AddAutofac())
       .UseStartup<Startup>();

On looking at a fresh v3.1 project, it had something like this instead:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });