在 Visual Studio Code 中设置 .NET Core MVC Web 应用

Posted

技术标签:

【中文标题】在 Visual Studio Code 中设置 .NET Core MVC Web 应用【英文标题】:Setting up a .NET Core MVC web app in Visual Studio Code 【发布时间】:2020-01-15 04:20:23 【问题描述】:

我一直在尝试使用 Visual Studio Code,并且正在努力按照入门指南启动和运行 .NET Core MVC Web 应用程序。

我安装了 Visual Studio Code、.NET Core SDK 和 C# 代码扩展,并使用 dotnet new mvc 命令创建了项目。我确保我按照提示添加了构建和调试所需的缺失资产。

此时,我阅读的教程说您可以使用 F5 运行应用程序,并且他们会在浏览器中显示一个模板页面。但是,当我尝试运行它时,它会将我带到 Environment 下拉菜单。如果我选择 .NET Core,它会将我带到一个 launch.json 文件。

我是 Visual Studio 代码的新手,因此我不确定如何最好地解决此问题,或者我是否遗漏或误解了某些内容。

【问题讨论】:

你能链接你使用的教程吗? 你的launch.json是什么样的? F5 = 在 Visual Studio 中调试。我什至不确定它是否是 Visual Studio Code 中的相同命令,但即使是,它也可以重新映射。在某个地方(在菜单、上下文菜单等中)你应该有一些东西可以让你“调试”。试试看;可能只是 F5 没有这样做。 【参考方案1】:

为了让 Visual Studio Code 运行和调试您的项目,您必须为每个项目创建一个启动配置。 (见:https://code.visualstudio.com/docs/editor/debugging#_launch-configurations)

从“调试”菜单中选择“添加配置...”并为环境选择“.NET Core”。

.NET Core MVC 项目的启动配置 (launch.json) 如下所示:


// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    
        "name": ".NET Core Launch (web)",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "$workspaceFolder/bin/Debug/netcoreapp2.2/Your.Application.dll",
        "args": [],
        "cwd": "$workspaceFolder",
        "stopAtEntry": false,
        "serverReadyAction": 
            "action": "openExternally",
            "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
        ,
        "env": 
            "ASPNETCORE_ENVIRONMENT": "Development"
        ,
        "sourceFileMap": 
            "/Views": "$workspaceFolder/Views"
        
    ,
    
        "name": ".NET Core Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "$command:pickProcess"
    ]

然后您可以从 Visual Studio Code 运行和调试您的 .NET Core MVC Web 应用程序!

【讨论】:

【参考方案2】:

感谢您的回复。 感谢您的反馈。我已经删除了测试项目,但是当我打开launch.json文件时,它有一些条目,但与上面的launch.json文件相比,它非常基本。例如,它缺少 env 和其他条目。

"env": 
            "ASPNETCORE_ENVIRONMENT": "Development"
        ,

我不知道为什么它没有生成这个。我曾多次尝试使用 dotnet new mvc 命令删除并重新创建项目,每次都一样。

但是,一周后我再次访问它,当我运行 dotnet new mvc 时,它会使用完整的 launch.json 创建应用程序,如上所示,并且它会首次运行。

【讨论】:

以上是关于在 Visual Studio Code 中设置 .NET Core MVC Web 应用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Visual Studio Code 中设置用于调试 PowerShell 的环境变量?

在 Mac 上的 Visual Studio Code 调试器中设置环境变量

在 Visual Studio Code 中设置 .NET Core MVC Web 应用

在 Visual Studio Code 中设置 Cobol 制表符/缩进

在基于 Visual Studio Code Maven 的 Java 项目中设置类路径

如何在 Visual Studio Code 中设置我的工作区文件夹?