我期待在vscode中运行asp.net mvc应用程序,但似乎我在google上找到的唯一页面是asp.net core,这不是我正在寻找的。有人可以指导我一些步骤,我安装了一些插件,如c#和msbuild。试图运行它之后。它显示以下错误:
“无法启动外部程序msbuild.spawn msbuild ENOENT”
我已经为我创建了一个处理构建的gulp文件:
你可以在我的project's Github上找到gulp文件
发生错误Failed to launch external program msbuild . spawn msbuild ENOENT
是因为vscode \ task runner找不到msbuild。
要在visual studio代码编辑器中运行asp.net mvc 4.5,您需要安装msbuild工具(我已经安装了2017版本)和IIS Express。
您可以使用vswhere来检查msbuild位置,在我的例子中是C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\msbuild.exe
在vscode中执行命令Tasks: Configure Task Runner
并根据文件编辑tasks.json的内容。
{
"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"
}
]
}
您无需在每次更改时重新启动IIS,只需构建应用程序CTRL+SHIFT+B
即可。
如果您不想停止IIS,请使用vscode命令Tasks: Terminate Running Task
。
参考文献:
根据VS Code文档,VS Code不支持调试Desktop .NET Framework上运行的应用程序。 VS Code无法识别ASP.NET MVC应用程序(虽然支持ASP.NET Core)。因此VS Code是编辑文件的轻量级工具,他们建议使用Visual Studio社区。
对于Visual Studio Code 1.30.2,我已将其配置为使用以下设置在IISExpress中构建和运行我的ASP.NET应用程序。
终端 - >配置任务
然后从模板条目中选择Create tasks.json file。
完成后,选择MSBuild模板
这将创建默认的MS构建任务模板。
您应该能够将以下内容复制到task.json文件中:
{
// 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"
}
}
]
}
保存文件后,只需按Ctrl + Shift + B并从窗口中选择Build任务。如果一切顺利,您应该看到下面的终端显示输出。
然后在IIS中启动您的应用程序转到终端 - >运行任务
然后该窗口将显示您的IIS Express任务,选择要启动的任务,您应该看到“输出”窗口显示IIS启动。一旦成功,只需打开浏览器并导航到localhost:{portyouconfigured},您应该看到您的应用程序正在运行。
自Visual Studio Code 1.14 有一种新的语法来创建任务。
在您的菜单中单击 终端 - >配置任务 在vscode中创建一个构建项目的任务,如下所示:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"args": [
"/property:GenerateFullPaths=true",
"/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"
}
]
}
使用的路径对应于Visual Studio 2017安装提供的路径。要找到它的msbuild路径,请进入文件资源管理器,然后搜索“msbuild.exe”。
我建议你最好在10分钟内安装dotnet cli,如页面所示。如果您正在使用Linux,因为我使用的是CentOS,并按照终端中的以下步骤操作:
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2
要确认dotnet-cli是否已成功安装:
dotnet
下面的命令在myWebApp文件夹中设置了一个基本的asp.net Web应用程序:dotnet new webApp -o myWebApp --no-https cd myWebApp
只需在终端输入dotnet run
即可运行您的第一个asp.net应用程序这使您的第一个项目成功运行。在我看来,使用Visual Studio代码比使用任何其他方法更好。我用这个link来源