【问题标题】:ASP.NET Core app in Visual Studio Codespaces debugging issueVisual Studio Codespaces 中的 ASP.NET Core 应用程序调试问题
【发布时间】:2020-11-29 22:35:09
【问题描述】:

我在 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 上运行它,它就可以工作。

【问题讨论】:

    标签: c# .net asp.net-core visual-studio-code codespaces


    【解决方案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核心启动以运行应用程序,Chrome使用http:// localhost:5000打开应用程序:5000 然后被重定向到 https://localhost:5001 因为

    app.UseHttpsRedirection();
    

    在我的配置中。

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

    【讨论】:

    • 您好,感谢您的回答!我试过了,但在我的情况下似乎不起作用。我应该配置其他东西吗?
    • 嗨,Ewean。用 VS Code 做了一些测试,并更新了我的答案。
    • 您好,我检查了文件,发现内容相同。(.vscode/tasks.json 和 .vscode/launch.json)。但是,当我在本地 Visual Studio Code 中运行时,它似乎可以工作。我不明白为什么 Codespace 不能重定向到我的本地主机。此外,如果我继续打开我的本地 Visual Studio,我可以从 Codespace 运行它。所以如果我需要运行和调试我的 web 应用程序,我需要打开一个本地的 Vs Code 吗?
    • 这个案例让我很好奇,所以我拿起我的信用卡并注册了 Azure。由于反复出现错误消息,尝试从 VS 和 VS 代码设置帐户是不可能的。但是,到目前为止我的发现:如果您在本地运行您的应用程序,然后单击 online.visualstudio.com 上的“端口 5000”-链接,您将被重定向到 localhost:5000。我在云方面的经验来自 AWS 上的 Kubernetes/OpenShift,而不是 Azure,但我认为在 Azure 中调试代码可能是不可能的。不过,我设法将我的小示例应用程序一直部署到 Azure 中的生产环境。
    • 谢谢! :) 它在端口 5000 上工作。我通过评论 app.UseHttpsRedirection(); 来管理它。
    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 2021-08-11
    • 2020-08-27
    • 2011-11-07
    • 2016-10-16
    • 2021-06-25
    • 2019-05-15
    • 2019-11-29
    相关资源
    最近更新 更多