Bill Kindig
Bill Kindig

Reputation: 3622

How can I debug .NET MVC project with VS Code

I have Visual Studio 2019 installed but I prefer to use VS Code. After hours of research, I'm confused as to what I can debug and what I cannot.

Are these instructions only for console applications?

https://github.com/OmniSharp/omnisharp-vscode/wiki/Desktop-.NET-Framework

I also found this:

How can we debug ASP.NET MVC web application in Visual Studio Code?

But it seems like the configuration.program needs to point to an EXE. Whereas my application generates a DLL.

How can I debug a .NET MVC (non-core) web application with VS Code?


Solution

I followed ruslan.gilmutdinov answer below and it's working! It wasn't as complicated as I thought it would be.

Upvotes: 3

Views: 2510

Answers (2)

Ruslan Gilmutdinov
Ruslan Gilmutdinov

Reputation: 1317

Let's assume your .NET Framework web application is called MyWebApp and you want to debug it on port 5521. This is how you can set up debugging in VSCode:

  1. Add OmniSharp extension to VSCode.

  2. Add following things to the PATH environment variable:

msbuild: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
iisexpress: C:\Program Files\IIS Express\
chrome: C:\Program Files (x86)\Google\Chrome\Application
  1. Build you project:
cd MyWebApp
msbuild MyWebApp.sln -m
  1. Open .vs\MyWebApp\config\applicationhost.config and set up your <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>

(pay attention to the values of attributes bindingInformation and physicalPath)

  1. Change your .csproj file. In VSCode, open up MyWebApp/MyWebApp.csproj. Look for this section and this is where you will be making some changes:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
</PropertyGroup>

Ensure that the Optimize is set to false and set the debug option to "portable":

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DebugType>portable</DebugType>
  <Optimize>false</Optimize>
</PropertyGroup>
  1. Add following launch.json and tasks.json to .vscode directory:

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"]
    }
  ]
}

You will get the following options for debugging in VSCode:

enter image description here

Now all you have to do is to launch Start WebApp and then Launch & Attach Chrome. Happy debugging!

Upvotes: 3

vithushan_ms
vithushan_ms

Reputation: 17

You can simply click the debug buttun on the vscode enter image description here

Then click create a launch.json file

After that, it will ask for the runtime. Select .NET for that and select your startup project. then you can see the launch.json file in your project path. Now add debug points wherever you want and press f5

Upvotes: 0

Related Questions