如何使用 VS Code 调试 .NET MVC 项目
How can I debug .NET MVC project with VS Code
我安装了 Visual Studio 2019,但我更喜欢使用 VS Code。经过几个小时的研究,我对什么可以调试什么不能调试感到困惑。
我安装了 C# 扩展。
我创建了一个 launch.json 并且
tasks.json.
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "clr",
"request": "launch",
"preLaunchTask": "build",
"program": "{path-to-my-project}.dll",
"args": [],
"cwd": "{myNameSpace}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\bNow listening on:\s+(https?://\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
},
"logging": {
"moduleLoad": false
}
},
{
"name": ".NET Core Attach",
"type": "clr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
这些说明仅适用于控制台应用程序吗?
https://github.com/OmniSharp/omnisharp-vscode/wiki/Desktop-.NET-Framework
我也发现了这个:
但是 configuration.program
似乎需要指向一个 EXE。而我的应用程序生成一个 DLL。
如何使用 VS Code 调试 .NET MVC(非核心)Web 应用程序?
解决方案
我遵循了下面的 ruslan.gilmutdinov 答案,它起作用了!它并没有我想象的那么复杂。
您只需单击 vscode 上的调试按钮即可
然后点击创建一个launch.json文件
之后,它会询问运行时间。 Select .NET 和 select 你的启动项目。然后你可以在项目路径中看到 launch.json 文件。现在在任何你想要的地方添加调试点并按 f5
假设您的 .NET Framework Web 应用程序名为 MyWebApp
,并且您想在端口 5521
上调试它。这是在 VSCode:
中设置调试的方法
将 OmniSharp 扩展添加到 VSCode。
在PATH环境变量中添加以下内容:
msbuild: C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin
iisexpress: C:\Program Files\IIS Express\
chrome: C:\Program Files (x86)\Google\Chrome\Application
- 建立你的项目:
cd MyWebApp
msbuild MyWebApp.sln -m
- 打开
.vs\MyWebApp\config\applicationhost.config
并设置您的 <site>
:
<sites>
<site name="MyWebApp" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="W:\Projects\NET\MyWebApp\MyWebApp" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5521:localhost" />
</bindings>
</site>
<siteDefaults>
<!-- To enable logging, please change the below attribute "enabled" to "true" -->
<logFile logFormat="W3C" directory="%AppData%\Microsoft\IISExpressLogs" enabled="false" />
<traceFailedRequestsLogging directory="%AppData%\Microsoft" enabled="false" maxLogFileSizeKB="1024" />
</siteDefaults>
<applicationDefaults applicationPool="Clr4IntegratedAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
(注意属性bindingInformation
和physicalPath
的值)
- 更改您的
.csproj
文件。在 VSCode 中,打开 MyWebApp/MyWebApp.csproj
。查找此部分,您将在此处进行一些更改:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
确保 Optimize
设置为 false 并将调试选项设置为“便携式”:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
- 将以下
launch.json
和 tasks.json
添加到 .vscode
目录:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "msbuild",
"command": "msbuild.exe",
"type": "shell",
"args": [
"/p:Configuration=Debug",
"/t:build",
"-m"
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "iisStart",
"type": "shell",
"dependsOn": "msbuild",
"command": "Start-Process",
"args": ["iisexpress.exe", "/config:${workspaceFolder}\.vs\MyWebApp\config\applicationhost.config /site:MyWebApp"],
"presentation": {
"reveal": "silent",
}
},
{
"label": "iisStop",
"type": "shell",
"command": "Stop-Process",
"args": ["-Name", "iisexpress"]
},
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Chrome",
"request": "launch",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}",
"runtimeArgs": [
"http://localhost:5521/",
"--new-window",
"--remote-debugging-port=9222"
]
},
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}"
},
{
"name": "Start WebApp",
"type": "clr",
"request": "attach",
"processName": "iisexpress",
"preLaunchTask": "iisStart",
"postDebugTask": "iisStop",
},
],
"compounds":[
{
"name": "Launch & Attach Chrome",
"configurations": ["Launch Chrome", "Attach to Chrome"]
}
]
}
您将在 VSCode 中获得以下调试选项:
现在您只需启动 Start WebApp
,然后启动 Launch & Attach Chrome
。
调试愉快!
我安装了 Visual Studio 2019,但我更喜欢使用 VS Code。经过几个小时的研究,我对什么可以调试什么不能调试感到困惑。
我安装了 C# 扩展。
我创建了一个 launch.json 并且 tasks.json.
{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "clr", "request": "launch", "preLaunchTask": "build", "program": "{path-to-my-project}.dll", "args": [], "cwd": "{myNameSpace}", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\bNow listening on:\s+(https?://\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" }, "logging": { "moduleLoad": false } }, { "name": ".NET Core Attach", "type": "clr", "request": "attach", "processId": "${command:pickProcess}" } ] }
这些说明仅适用于控制台应用程序吗?
https://github.com/OmniSharp/omnisharp-vscode/wiki/Desktop-.NET-Framework
我也发现了这个:
但是 configuration.program
似乎需要指向一个 EXE。而我的应用程序生成一个 DLL。
如何使用 VS Code 调试 .NET MVC(非核心)Web 应用程序?
解决方案
我遵循了下面的 ruslan.gilmutdinov 答案,它起作用了!它并没有我想象的那么复杂。
您只需单击 vscode 上的调试按钮即可
然后点击创建一个launch.json文件
之后,它会询问运行时间。 Select .NET 和 select 你的启动项目。然后你可以在项目路径中看到 launch.json 文件。现在在任何你想要的地方添加调试点并按 f5
假设您的 .NET Framework Web 应用程序名为 MyWebApp
,并且您想在端口 5521
上调试它。这是在 VSCode:
将 OmniSharp 扩展添加到 VSCode。
在PATH环境变量中添加以下内容:
msbuild: C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin
iisexpress: C:\Program Files\IIS Express\
chrome: C:\Program Files (x86)\Google\Chrome\Application
- 建立你的项目:
cd MyWebApp
msbuild MyWebApp.sln -m
- 打开
.vs\MyWebApp\config\applicationhost.config
并设置您的<site>
:
<sites>
<site name="MyWebApp" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="W:\Projects\NET\MyWebApp\MyWebApp" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5521:localhost" />
</bindings>
</site>
<siteDefaults>
<!-- To enable logging, please change the below attribute "enabled" to "true" -->
<logFile logFormat="W3C" directory="%AppData%\Microsoft\IISExpressLogs" enabled="false" />
<traceFailedRequestsLogging directory="%AppData%\Microsoft" enabled="false" maxLogFileSizeKB="1024" />
</siteDefaults>
<applicationDefaults applicationPool="Clr4IntegratedAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
(注意属性bindingInformation
和physicalPath
的值)
- 更改您的
.csproj
文件。在 VSCode 中,打开MyWebApp/MyWebApp.csproj
。查找此部分,您将在此处进行一些更改:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
确保 Optimize
设置为 false 并将调试选项设置为“便携式”:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
- 将以下
launch.json
和tasks.json
添加到.vscode
目录:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "msbuild",
"command": "msbuild.exe",
"type": "shell",
"args": [
"/p:Configuration=Debug",
"/t:build",
"-m"
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "iisStart",
"type": "shell",
"dependsOn": "msbuild",
"command": "Start-Process",
"args": ["iisexpress.exe", "/config:${workspaceFolder}\.vs\MyWebApp\config\applicationhost.config /site:MyWebApp"],
"presentation": {
"reveal": "silent",
}
},
{
"label": "iisStop",
"type": "shell",
"command": "Stop-Process",
"args": ["-Name", "iisexpress"]
},
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Chrome",
"request": "launch",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}",
"runtimeArgs": [
"http://localhost:5521/",
"--new-window",
"--remote-debugging-port=9222"
]
},
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}"
},
{
"name": "Start WebApp",
"type": "clr",
"request": "attach",
"processName": "iisexpress",
"preLaunchTask": "iisStart",
"postDebugTask": "iisStop",
},
],
"compounds":[
{
"name": "Launch & Attach Chrome",
"configurations": ["Launch Chrome", "Attach to Chrome"]
}
]
}
您将在 VSCode 中获得以下调试选项:
现在您只需启动 Start WebApp
,然后启动 Launch & Attach Chrome
。
调试愉快!