asp.net-mvc 如何在 Visual Studio 代码编辑器中运行 asp.net mvc 4.5?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42296428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:36:07  来源:igfitidea点击:

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

asp.net-mvcvisual-studio-code

提问by hmota

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:

我期待在 vscode 中运行 asp.net mvc 应用程序,但似乎我在 google 上找到的唯一页面是带有 asp.net core 的页面,这不是我想要的。有人可以指导我一些步骤,我安装了一些插件,如 c# 和 msbuild。尝试运行后。它显示以下错误:

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

“无法启动外部程序 msbuild。产生 msbuild ENOENT”

回答by Saint Play

I've created a gulpfilethat handle the build for me:

我创建了一个gulpfile来为我处理构建:

  1. It starts an IISExpress instance.
  2. Refresh my browser on razorcode change.
  3. And automatically rebuild my application when I change C# code.
  1. 它启动一个 IISExpress 实例。
  2. 剃刀代码更改时刷新我的浏览器。
  3. 并在我更改 C# 代码时自动重建我的应用程序。

You can find the gulpfileon my project's Github

你可以在我项目的 Github上找到gulpfile

回答by Ricardo Fontana

The error Failed to launch external program msbuild . spawn msbuild ENOENThappens because vscode\task runnercannot find msbuild.

Failed to launch external program msbuild . spawn msbuild ENOENT发生错误是因为vscode\task runner找不到 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.

要在 Visual Studio 代码编辑器中运行 asp.net mvc 4.5,您需要安装 msbuild 工具(我已经安装了 2017 版本)和 IIS Express。

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

您可以使用vswhere检查 msbuild 位置,在我的情况下是C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\msbuild.exe

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

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

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

您不需要在每次更改时重新启动 IIS,您只需要构建应用程序CTRL+SHIFT+B

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

如果您不想停止 IIS,请使用 vscode 命令Tasks: Terminate Running Task

References:

参考:

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

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

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

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

回答by Stephy

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.

根据 VS Code 文档,VS Code 不支持调试在桌面 .NET Framework 上运行的应用程序。VS Code 无法识别 ASP.NET MVC 应用程序(尽管支持 ASP.NET Core)。因此 VS Code 是编辑文件的轻量级工具,他们推荐使用 Visual Studio 社区。

回答by R007

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.

对于 Visual Studio Code 1.30.2,我已将其配置为使用以下设置在 IISExpress 中构建和运行我的 ASP.NET 应用程序。

Terminal -> Configure Tasks

终端 -> 配置任务

Then select Create tasks.json file from template entry.

然后选择Create tasks.json file from template entry

Once you do that then select the MSBuild template

一旦你这样做,然后选择 MSBuild 模板

enter image description here

在此处输入图片说明

This will create the default MS build task template.

这将创建默认的 MS 构建任务模板。

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

您应该能够将以下内容复制到 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"
            }
        }
    ]
}

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.

保存文件后,只需按 Ctrl + Shift + B 并从窗口中选择 Build 任务。如果一切顺利,您应该会在下面的终端中看到输出。

enter image description here

在此处输入图片说明

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

然后要在 IIS 中启动您的应用程序,请转到终端 -> 运行任务

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.

然后该窗口将显示您的 IIS Express 任务,选择您想要启动的任务,您应该会看到输出窗口显示 IIS 正在启动。一旦成功,只需打开浏览器并导航到 localhost:{portyouconfigured},您应该会看到您的应用程序正在运行。

enter image description here

在此处输入图片说明

回答by muhammad tayyab

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:

我最好建议您安装 dotnet cli,以便在页面上说的 10 分钟内开始使用它。如果您像我使用 CentOS 一样使用 Linux 并在终端中执行以下步骤:

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-cli 是否安装成功:

dotnet

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

下面的命令在 myWebApp 文件夹中设置了一个基本的 asp.net web 应用程序: dotnet new webApp -o myWebApp --no-https cd myWebApp

Simply type dotnet runin 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 linksource

只需dotnet run在终端中输入即可运行您的第一个 asp.net 应用程序这将使您的第一个项目成功运行。在我看来,这比任何其他方法更适用于 Visual Studio 代码。我使用这个链接

回答by Dayan

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

我知道我有点晚了,但是在 2019 年自己研究了这个之后,我认为使用 VSCode 的任务是一个更好的方法。

Create a tasks.jsonfile inside your project's .vscodefolder, this is where the below code will live.

tasks.json在您的项目.vscode文件夹中创建一个文件,这是以下代码所在的位置。

enter image description here

在此处输入图片说明

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

将以下脚本添加到tasks.json,我正在使用它来构建项目,然后运行 ​​iisexpress。

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

保存后,您可以Build & Run ServerCTRL+ SHIFT+运行任务B。你也可以用访问可用的命令CTRL+ SHIFT+P和搜索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

在此处输入图片说明

回答by etiennejcharles

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

从 Visual Studio Code 1.14 开始,
有一种新的语法来创建任务。

In your MenuClick on

Terminal -> Configure Tasks

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

在您的菜单中单击

终端 -> 配置任务

在 vscode 中创建一个任务来构建您的项目,如下所示:

{
  "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".

使用的路径对应于 Visual Studio 2017 安装提供的路径。要找到它的 msbuild 路径,请进入文件资源管理器,然后搜索“msbuild.exe”。