Build and Debug ASP.NET MVC 5 Framework Web Application 4.x in VS Code

Some of us are stuck with older, brownfield web projects based on Framwork 4.x that has to be supported yet for a while. But we’ve also tasted the sweetness of the still not bloated (dream on suckers, it will be bloated) VS Code and want to use it while it’s still pristine and fast. But is it possible?

Yes, it’s possible

It requires some editing with the launch.json and tasks.json files in the .vscode folder. Hang on and note that I just got this running last night.

Fist, open the folder where your “old” web application is located. In the same directory as your .sln file, create a folder named .vscode. In the .vscode folder, a file called tasks.json, which describes one or more build steps.

tasks.json

{

    "version": "2.0.0",

    "tasks": [

        {

            "label": "build",

            "type": "shell",

            "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\MSBuild\\Current\\Bin\\msbuild.exe",

            "args": [

                "${workspaceFolder}/WebApplication1/WebApplication1.csproj",

                "/t:build",

                "/p:GenerateFullPaths=true",

                "/p:Configuration=Debug",

                "/consoleloggerparameters:NoSummary"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            }

            "presentation": {

                "reveal": "silent"

            },

            "problemMatcher": "$msCompile"

        },

    ]

}


Of course, you will have to replace “WebApplication1” with your project name, and you may also have to change the path to which ever msbuild.exe you want or need to run (bold, italic above).

To build i VS Code, press CTRL+SHIFT+B and your project should do just that. The task we defined is set to be the default, so VS Code will not ask you for anything, just build. Hopefully.

Run in IIS Express, 64-bit

To run this application in IIS Express, add the following task to tasks.json “tasks” array:

tasks.json (part)

        … stuff before…

        {

            "label": "iisexpress",

            "type": "shell",

            "command": "C:\\Program Files\\IIS Express\\iisexpress.exe",

            "args": [

                "/path:${workspaceRoot}\\WebApplication1",

                "/port:5006"

            ],

            "presentation": {

                "reveal": "silent"

            },

            "problemMatcher": "$msCompile"

        },
       …stuff after…


Again – replace “WebApplication1” with your project name.

Note that the iisexpress.exe we point at MUST be the 64-bit version, or we won’t be able to debug later on. The 64-bit version is located in c:\program files\ directory, and the 32-bit version in – you guessed it – the c:\program files(x86)\ directory.

To get IIS Express going from VS Code, press CTRL+SHIFT+P, to bring up the command prompt, then type/select “Tasks: Run Task” and select “iisexpress”.

You should see something like this in the VS Code terminal window (you may have to bring it up):

Starting IIS Express ...
Successfully registered URL "
http://localhost:5006/" for site "Development Web Site" application "/"
Registration completed
IIS Express is running.
Enter 'Q' to stop IIS Express


You can now open a browser and point at http://localhost:5006, your web application should hopefully start.

Debugging

Debugging in VS Code is defined in the launch.json file. VS Code has to “attach” to the iisexpress.exe process. So create a launch.json file in .vscode directory, and add this:

launch.json

{

    "version": "0.2.0",

    "configurations": [

      {

        "name": ".NET Attach",

        "type": "clr",

        "request": "attach",

        "processName": "iisexpress.exe",

      }

    ]

  }

 


To “attach” and debug our running application. Press F5 in VS Code. If all is well, the debugger should start running, and you should be able to set breakpoints and whatnot. I’ve not looked at how to debug *.cshtml files yet, but I’m sure it can be done if needed.

Build, Run and Browse

It’s possible to do build, run in iis express and start Chrome/Edge in one step for convenience. This is my full tasks.json file for that. On F5 it will run the default task “build run browse” which depends on the tasks “build”, “iisexpress” and “browser”. You might want to exclude the “browser” subtask, but here it is:

{

    // See https://go.microsoft.com/fwlink/?LinkId=733558

    // for the documentation about the tasks.json format

    //https://bigfont.ca/debug-an-iis-express-web-application-from-vs-code/

    //appcmd add site /name:"WebApplication1" /bindings:https/*5006:localhost /physicalPath:C:\code-test\framework\WebApplication1\WebApplication1\obj\Debug\Package\PackageTmp

    //"C:\Program Files (x86)\IIS Express\appcmd.exe" add site /name:"WebApplication1" /bindings:https/*5006:localhost /physicalPath:C:\code-test\framework\WebApplication1\WebApplication1\obj\Debug\Package\PackageTmp

    "version": "2.0.0",

    "tasks": [

        {

            "label": "build run browse",

            "dependsOn": [

               "build",

               "iisexpress",

               "browser"

            ],

            "group": {

               "kind": "build",

               "isDefault": true

            }

        },

        {

            "label": "build",

            "type": "shell",

            "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\MSBuild\\Current\\Bin\\msbuild.exe",

            "args": [

               "${workspaceFolder}/WebApplication1/WebApplication1.csproj",

               "/t:build",

               "/p:GenerateFullPaths=true",

               "/p:Configuration=Debug",

               "/consoleloggerparameters:NoSummary"

            ],

            "group": "build",

            "presentation": {

               "reveal": "silent"

            },

            "problemMatcher": "$msCompile"

        },

        {

            "label": "iisexpress",

            "type": "shell",

            "command": "C:\\Program Files\\IIS Express\\iisexpress.exe",

            "args": [

               "/path:${workspaceRoot}\\WebApplication1",

               "/port:5006"

            ],

            "presentation": {

               "reveal": "silent"

            },

            "problemMatcher": "$msCompile"

        },

        {

            "label": "browser",

            "type": "shell",

            "command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",

            "args": [

               "http://localhost:5006"

           ],

           "problemMatcher": []

       }

   ]

}

 

 

 

Hopefully this might help some of you out there to use VS Code with Framework projects and transition over to .net 5 or 6. VS Code is still fast and really, really useful, though VS 2022 looks awesome and seems to outshine VS Code for me.

1 Comment

Comments have been disabled for this content.