【问题标题】:How to properly debug an ASP.NET MVC Core with Visual Studio Code如何使用 Visual Studio Code 正确调试 ASP.NET MVC Core
【发布时间】:2021-09-17 14:38:23
【问题描述】:

我在使用 Visual Studio Code 调试 MVC ASP.NET Core 应用程序时遇到了困难。

我正在打开包含我想在 VS Code 中运行的项目的文件夹。

.csproj 文件的相关部分如下:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>aspnet-XXXXXXX-941AF9EA-C0DF-419D-B0F8-69FE3A477A65</UserSecretsId>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>$(SolutionDir)Builds\Debug\</OutputPath>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
  </PropertyGroup>
</Project>

现在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": "watch",
      // If you have changed target frameworks, make sure to update the program path.
      "program": "${workspaceFolder}/Builds/Debug/XXXXXXX.dll",
      "args": [],
      "cwd": "${workspaceFolder}",
      "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}"
    }
  ]
}

现在,当我打开一个代码文件,设置断点并选择启动应用程序时,编译发生但浏览器最终没有运行......

更令人惊讶的是,构建的输出会发送到终端,而不是调试控制台:

Executing task: dotnet watch run /Users/omatrot/Documents/bealink/bealink_server/XXXXXXX/XXXXXXX/XXXXXXX.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary <

然后我按 CTRL+C 停止托管进程...
然后 VS Code 立即再次运行它,这次在调试控制台中输出,打开浏览器,并在我的断点处停止。

这对我来说似乎很疯狂。

编辑 1:这在 Visual Studio 下通过删除以下参考有效:

<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />

【问题讨论】:

    标签: debugging visual-studio-code asp.net-core-mvc vscode-debugger


    【解决方案1】:

    您需要先阅读official doc,并知道我们需要在 vscode 中添加一些扩展。

    你也可以read Jason Watmore's blog,在博客中详细介绍了如何调试。您可以创建 task.json 和 launch.json。还有视频教程供我们参考。

    【讨论】:

    • 我已经这样做了。我可能对此不是很清楚,但我正在处理一个复杂的现有项目,而不是一个始终有效的简单项目。
    • @OlivierMATROT 你能看看这个博客吗,wakeupandcode.com/…