【问题标题】:Visual Studio ASP.NET Core Debug IIS Express - Site can't be reachedVisual Studio ASP.NET Core 调试 IIS Express - 无法访问站点
【发布时间】:2021-02-10 19:44:14
【问题描述】:

我一直在 Windows 10 PC 上的 VS 2019 中开发 ASP.NET Core 2.1 Web 应用程序。一切正常,在 Chrome/IIS Express 中调试也没有问题。

我将我的项目复制到我刚开始使用的新 Win 10 计算机上。

当我尝试在 Chrome 中使用 IIS Express 调试项目时,我收到一个错误页面,上面写着:无法访问此站点。

我遵循了溢出文章中的各种建议修复,但似乎没有任何效果。这让我发疯,浪费了这么多时间。我已经尝试了所有这些步骤,但没有任何乐趣:

  • 禁用防病毒防火墙/保护
  • 检查项目属性(与以前的 PC 相同)
  • 检查launchSettings.json文件(与上一台PC相同)
  • 删除 vs/config 文件夹中的 applicationhost.config(帖子说此文件将在重建时重新创建,但重建后文件仍然不存在!)
  • 删除 obj 文件夹并重建
  • 删除 vs 文件夹并重建
  • 检查 Windows 功能(与运行正常的旧 PC 相同)
  • 更改 VS 选项/调试:取消选中启用编辑并继续复选框
  • 以管理员身份运行 cmd:cd "C:\Program Files (x86)\IIS Express" IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:44301/ -UseSelfSigned

launchSettings.json 文件

launchSettings.json
{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "https://localhost:44301/",
      "sslPort": 44376
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "CanvasWeb": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:61900/"
    }
  }
}

更新

我尝试了以下操作,但仍未解决问题:

  • 运行修复 IIS Express
  • 检查证书存储中没有重复的证书
  • 已删除证书以使 IIS 生成新证书
  • 创建了一个新的“测试”项目 - 出现同样的问题

【问题讨论】:

  • 您是否尝试过不使用 IIS express 启动?您可以通过更改个人资料来做到这一点
  • @CamiloTerevinto 如果我将配置文件更改为唯一的其他可用选项,它会将 Chrome 打开到本地主机并仍然说:无法访问站点。如果我随后将应用程序 URL 更改为:localhost:61900 我得到一个异常 - System.InvalidOperationException: 'HTTPS 端点只能使用 KestrelServerOptions.Listen() 进行配置。'
  • 您可能在您的 Program 类中通过 IIS express 强制 HTTPs,当您创建项目时它默认完成 IIRC。您还可以做 2 件事:从模板创建一个新的 asp.net 核心项目并查看错误是否仍然发生(如果是,您知道问题出在其他地方),并在通过 IIS Express 启动时检查 VS 的输出窗口。这可能需要半个多小时,但(几乎)最后的手段是启动 Visual Studio 安装程序并修复安装

标签: visual-studio asp.net-core https iis-express


【解决方案1】:

问题可能是 IIS Express 的 HTTPS 错误。 要检查它: 创建一个没有 Https 的新 ASP.NET Core Web 应用程序。 在 Web 应用程序模板中取消选中 /deselect configure for https Uncheck configure for https

如果新的 Web 应用程序运行,那么它是 IIS Express HTTPS 问题。 使用管理员 previliadge 运行流动的 power shell 代码将解决 IIS Express 问题。

# code start
Start-Transcript -Path "$($MyInvocation.MyCommand.Path).log"
try {
    Write-Host "Creating cert resources"
    $ekuOidCollection = [System.Security.Cryptography.OidCollection]::new();
    $ekuOidCollection.Add([System.Security.Cryptography.Oid]::new("1.3.6.1.5.5.7.3.1","Server Authentication")) | Out-Null
    $sanBuilder = [System.Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new();
    $sanBuilder.AddDnsName("localhost") | Out-Null
    
    Write-Host "Creating cert extensions"
    $certificateExtensions = @(
        # Subject Alternative Name
        $sanBuilder.Build($true),        
        # ASP.NET Core OID
        [System.Security.Cryptography.X509Certificates.X509Extension]::new(
            "1.3.6.1.4.1.311.84.1.1",
            [System.Text.Encoding]::ASCII.GetBytes("IIS Express Development Certificate"),
            $false),
            # KeyUsage
            [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new(
                [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyEncipherment,
                $true),
                # Enhanced key usage
        [System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new(
            $ekuOidCollection,
            $true),
            # Basic constraints
            [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($false,$false,0,$true)
        )
    Write-Host "Creating cert parameters"
    $parameters = @{
        Subject = "localhost";
        KeyAlgorithm = "RSA";
        KeyLength = 2048;
        CertStoreLocation = "Cert:\LocalMachine\My";
        KeyExportPolicy = "Exportable";
        NotBefore = Get-Date;
        NotAfter = (Get-Date).AddYears(1);
        HashAlgorithm = "SHA256";
        Extension = $certificateExtensions;
        SuppressOid = @("2.5.29.14");
        FriendlyName = "IIS Express Development Certificate"
    }
    Write-Host "Creating cert"
    $cert = New-SelfSignedCertificate @parameters

    $rootStore = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList Root, LocalMachine
    $rootStore.Open("MaxAllowed")
    $rootStore.Add($cert)
    $rootStore.Close()
    
    Write-Host "Creating port bindings"
    # Add an Http.Sys binding for port 44300-44399
    $command = 'netsh'
    for ($i=44300; $i -le 44399; $i++) {
        $optionsDelete = @('http', 'delete', 'sslcert', "ipport=0.0.0.0:$i")
        $optionsAdd = @('http', 'add', 'sslcert', "ipport=0.0.0.0:$i", "certhash=$($cert.Thumbprint)", 'appid={214124cd-d05b-4309-9af9-9caa44b2b74a}')
        Write-Host "Running $command $optionsDelete"
        & $command $optionsDelete
        Write-Host "Running $command $optionsAdd"
        & $command $optionsAdd
    } 
}
catch {
    Write-Error $_.Exception.Message
}
finally {
    Stop-Transcript
}
# code End

参考: HTTPS Error using IIS Express #26437

【讨论】:

  • 感谢您发布此信息 - 这似乎是一种享受。
【解决方案2】:

我实际上遇到了这个问题并在互联网上做了一些研究,但是我找不到解决方案。然后,我决定关闭我的防病毒软件(卡巴斯基安全软件 21.2)并检查它是否可以解决问题,之后一切都开始工作了!不幸的是,我找不到负责网络活动的防病毒设置,所以即使是现在,如果我在 .NET 中测试后端 API,我必须关闭防病毒一段时间((

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多