25

I am looking forward to run asp.net mvc apps in vscode but it seems that the only pages that I found on google is with asp.net core which is not what I am looking. Can someone guide me with some steps, I installed some plugins like c# and msbuild. After attempt to run it. it display the following error:

"Failed to launch external program msbuild . spawn msbuild ENOENT"

hmota
  • 379
  • 1
  • 4
  • 10
  • 1
    try VS Community Edition instead (if you don't want to pay for VS). Will probably save yourself a big headache. VS code editor is not intended to do what you want. – ADyson Feb 17 '17 at 11:29
  • Yeah I guess so. Well thank you anyways. – hmota Feb 17 '17 at 15:58

7 Answers7

16

The error Failed to launch external program msbuild . spawn msbuild ENOENT happens because vscode\task runner cannot find msbuild.

To run asp.net mvc 4.5 in visual studio code editor, you will need to install msbuild tools (I have installed the 2017 version) and IIS Express.

You could use vswhere to check msbuild location, in my case is C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\msbuild.exe

In vscode execute the command Tasks: Configure Task Runner and edit the content of tasks.json according the file.

{
    "version": "0.1.0",
    "taskSelector": "/t:",
    "showOutput": "silent",
    "tasks": [
        {
            "taskName": "build",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true"
            ],
            "windows": {
                // change according your msbuild location
                "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\msbuild.exe"
            },
            // Show the output window only if unrecognized errors occur.
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        },
        {
            "suppressTaskName": true,
            "taskName": "iisexpress",
            "isShellCommand": true,
            "windows": {
                "command": "C:\\Program Files (x86)\\IIS Express\\iisexpress.exe"
            },
            "args": [
                // change according your project folder and desired port
                "/path:${workspaceRoot}\\MyProjectFolder",
                "/port:51714"
            ],
            // Show the iisexpress output always.
            "showOutput": "always"
        }
    ]
}

You don't need to restart your IIS on every change, you just need to build the application CTRL+SHIFT+B.

If you wan't to stop IIS use the vscode command Tasks: Terminate Running Task.

References:

https://stackoverflow.com/a/42719644/5270073

https://learn.microsoft.com/en-us/iis/extensions/using-iis-express/running-iis-express-from-the-command-line

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Ricardo Fontana
  • 4,583
  • 1
  • 21
  • 32
15

I've created a gulpfile that handle the build for me:

  1. It starts an IISExpress instance.
  2. Refresh my browser on razor code change.
  3. And automatically rebuild my application when I change C# code.

You can find the gulpfile on my project's Github

Saint Play
  • 1,083
  • 10
  • 12
11

As per VS Code documentation, VS Code does not support debugging applications running on the Desktop .NET Framework. The ASP.NET MVC Application (though ASP.NET Core is supported) are not recognized by VS Code. Hence VS Code is lightweight tool to edit a file, they are recommending to use Visual Studio Community.

Sudheesh C R
  • 219
  • 2
  • 11
6

For Visual Studio Code 1.30.2 I've got it configured to build and run my ASP.NET applications in IISExpress using the following setup.

Terminal -> Configure Tasks

Then select Create tasks.json file from template entry.

Once you do that then select the MSBuild template

enter image description here

This will create the default MS build task template.

You should be able to copy the following to the task.json file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        //Task for building your ASP.NET Solution
        {
            "label": "build",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build"
            ],
            "windows": {
                "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\msbuild.exe"
            },
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "always"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        },
        //Task for running App in IIS Express
        //You can add additional projects here if you want to run more than one project in IIS Express
        //For example this shows how I'm running my WebApp and API locally in IIS Expresse
        {
            "label": "iisexpress-WebApp",
            "type": "shell",
            "windows": {
                "command": "C:\\Program Files (x86)\\IIS Express\\iisexpress.exe"
            },
            "args":[
                "/path:${workspaceRoot}\\Web",
                "/port:52945"
            ],
            "presentation": {
                "reveal": "always"
            }
        },
        {
            "label": "iisexpress-API",
            "type": "shell",
            "windows": {
                "command": "C:\\Program Files (x86)\\IIS Express\\iisexpress.exe"
            },
            "args":[
                "/path:${workspaceRoot}\\Api",
                "/port:49243"
            ],
            "presentation": {
                "reveal": "always"
            }
        }
    ]
}

Once you save the file just hit Ctrl + Shift + B and select the Build task from the window. If all goes well you should see the output displayed in the terminal below.

enter image description here

Then to spin up your Apps in IIS go to Terminal -> Run Task

That window will then show your IIS Express tasks, select the one you want to spin up and you should see the Output window show IIS starting up. Once that is successful just open your browser and navigate to localhost:{portyouconfigured} and you should see your application running.

enter image description here

Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
R007
  • 378
  • 4
  • 11
  • Great answer, tnx! Just edit path to your `visual studio msbuild.exe` and path in your workspaceRoot to where your web.config file is – Tadija Bagarić Feb 16 '19 at 13:56
  • How can I set the arguments `/p:Configuration=Debug /p:Platform="AnyCPU"` ?? – Junior Usca May 29 '19 at 14:36
  • I have the application app and running. so how to debug my application? should i need attach it to any process? when i attach to iisexpress process and set breakpoints its showing unverified breakpoint. pardon me am new to set and debug these legacy application using vscode – Share_Improve Aug 06 '19 at 11:53
  • If it's not a .NET CORE app then you can't debug it with visual studio code. – R007 Aug 07 '19 at 12:03
  • Mark! My some back-end projects are not .net core and I don't want to run vs – sknight Aug 28 '19 at 08:19
2

I know I'm a little late, but after researching this myself in 2019, I think using VSCode's tasks is a better approach.

Create a tasks.json file inside your project's .vscode folder, this is where the below code will live.

enter image description here

Add the following script to tasks.json, I'm using this to build the project and then run iisexpress.

Once saved, you can run the Build & Run Server task by pressing pressing CTRL+SHIFT+B. You can also just access the available commands with CTRL+SHIFT+P and search for task: run build task.

{
        // See https://go.microsoft.com/fwlink/?LinkId=733558 
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Build & Run Server",
                "dependsOrder": "sequence",
                "group": "build",
                "dependsOn":["Build ASP.NET", "Run IIS EXPRESS"]
            },
            {
                "type": "shell",
                "label": "Build ASP.NET",
                "group": "build",
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared",
                    "showReuseMessage": true,
                    "clear": true
                },
                "args": [
                    // Ask msbuild to generate full paths for file names.
                    "/property:GenerateFullPaths=true"
                ],
                "windows": {
                    // change according your msbuild location
                    "command":"${env:ProgramFiles(x86)}\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\msbuild.exe"
                },
                // Use the standard MS compiler pattern to detect errors, warnings and infos
                "problemMatcher": "$msCompile"
            },
            {
                "type":"shell",
                "label": "Run IIS EXPRESS",
                "group": "build",
                "windows": {
                    "command": "C:\\Program Files (x86)\\IIS Express\\iisexpress.exe"
                },
                "args": [
                    // change according your project folder and desired port
                    "/path:${workspaceRoot}\\VSSMVCProj",
                    "/port:59010"
                ],
                // Show the iisexpress output always.
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared",
                    "showReuseMessage": true,
                    "clear": false
                },
            }
        ],

    }

enter image description here

RobC
  • 22,977
  • 20
  • 73
  • 80
Dayan
  • 7,634
  • 11
  • 49
  • 76
1

Since Visual Studio Code 1.14
There is a new syntax to create tasks.

In your Menu Click on

Terminal -> Configure Tasks

Create a task in vscode that build your project like so:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "args": [
        "${workspaceRoot}\\Some\\Window\\Path\\ToSolution.sln",
        "/target:Build"
      ],
      // Path to your msbuild
      // The path used corresponds to the path provided by a Visual Studio 2017 installation.
      // To find it your msbuild path, go in your file explorer, and search for "msbuild.exe".
      "windows": {
        "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\msbuild.exe"
      },
      "problemMatcher": "$msCompile"
    }
  ]
}

The path used corresponds to the path provided by a Visual Studio 2017 installation. To find it your msbuild path, go in your file explorer, and search for "msbuild.exe".

etiennejcharles
  • 359
  • 2
  • 10
0

I would better recommend you installing dotnet cli to get started on it within 10 minutes as page says. If you're using Linux as I was using CentOS and followed the steps below in terminal:

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2

To confirm if dotnet-cli installed successfully:

dotnet

The commmand below sets up a basic asp.net web application in myWebApp folder: dotnet new webApp -o myWebApp --no-https cd myWebApp

Simply type dotnet run in terminal to run your first asp.net app This makes your first project run sucessfully. In my opinion, this would work better with Visual studio code than any other method. I use this link source

muhammad tayyab
  • 727
  • 7
  • 18