Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run asp.net mvc 4.5 in visual studio code editor?

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"

like image 679
hmota Avatar asked Feb 17 '17 11:02

hmota


4 Answers

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

like image 162
Saint Play Avatar answered Oct 04 '22 08:10

Saint Play


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://docs.microsoft.com/en-us/iis/extensions/using-iis-express/running-iis-express-from-the-command-line

like image 33
Ricardo Fontana Avatar answered Oct 04 '22 10:10

Ricardo Fontana


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.

like image 11
Sudheesh C R Avatar answered Oct 04 '22 10:10

Sudheesh C R


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

like image 6
R007 Avatar answered Oct 04 '22 09:10

R007