1

我在 Visual Studio Codespaces 中创建了一个 ASP.NET Core 应用程序。我在项目中为 Visual Studio Code 添加了 C#,当我运行应用程序时,我没有重定向到本地 Web 应用程序。我有一个 HTTP 错误 504。 在此处输入图像描述

该页面正在阻止重定向。 在此处输入图像描述

然后是 HTTP 错误 504。 在此处输入图像描述

我的启动设置在端口 5001 和 5000 上配置:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:55448",
      "sslPort": 44383
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Test": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Codespace 中 Visual Studio Code 中的转发端口默认为:

在此处输入图像描述

任何线索为什么我无法通过地址 http://localhost:5000 访问 Web 应用程序?

编辑 :

.vscode/launch.json

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/Test/bin/Debug/netcoreapp3.1/Test.dll",
            "args": [],
            "cwd": "${workspaceFolder}/Test",
            "stopAtEntry": false,
            // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

.vscode/task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/Test/Test.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/Test/Test.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "${workspaceFolder}/Test/Test.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

此外,如果我在 VS Code 中打开 Codespace 并在本地 VS Code 上运行它,它就可以工作。 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

1

在本地使用 VS Code 时,不使用 launchSettings.json。在 VS Code 中安装 C# 扩展时,我在选择 .NET Core 启动时得到 .vscode/launch.json 和 .vscode/tasks.json(我的应用程序称为“WebApplication”)。

.vscode/launch.json(使用工作设置更新)

{
"version": "0.2.0",
"configurations": [
    {
        "name": ".NET Core Launch (web)",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/WebApplication.dll",
        "args": [],
        "cwd": "${workspaceFolder}",
        "stopAtEntry": false,
        "serverReadyAction": {
            "action": "openExternally",
            "pattern": "\\bNow listening on:\\s+(http?://\\S+)"
        },
        "env": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "sourceFileMap": {
            "/Views": "${workspaceFolder}/Views"
        }
    },
    {
        "name": ".NET Core Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "${command:pickProcess}"
    }
]
}

.vscode/tasks.json

{
"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "command": "dotnet",
        "type": "process",
        "args": [
            "build",
            "${workspaceFolder}/WebApplication.csproj",
            "/property:GenerateFullPaths=true",
            "/consoleloggerparameters:NoSummary"
        ],
        "problemMatcher": "$msCompile"
    },
    {
        "label": "publish",
        "command": "dotnet",
        "type": "process",
        "args": [
            "publish",
            "${workspaceFolder}/WebApplication.csproj",
            "/property:GenerateFullPaths=true",
            "/consoleloggerparameters:NoSummary"
        ],
        "problemMatcher": "$msCompile"
    },
    {
        "label": "watch",
        "command": "dotnet",
        "type": "process",
        "args": [
            "watch",
            "run",
            "${workspaceFolder}/WebApplication.csproj",
            "/property:GenerateFullPaths=true",
            "/consoleloggerparameters:NoSummary"
        ],
        "problemMatcher": "$msCompile"
    }
]
}

选择 .NET Core 启动运行应用程序时,Chrome 会使用 http://localhost:5000 打开应用程序,然后重定向到 https://localhost:5001,因为

app.UseHttpsRedirection();

在我的配置中。

也许尝试在本地从 VS Code 运行应用程序以查看是否有效?我还没有帐户可以在 Codespaces 中对此进行测试,对此我深表歉意。

于 2020-08-10T12:42:48.527 回答