Search code examples
c#.net-corevisual-studio-codeasp.net-core-mvc

Setting up a .NET Core MVC web app in Visual Studio Code


I've been trying to play around with Visual Studio Code and am struggling to get up and running with a .NET Core MVC web app following the getting started guides.

I installed Visual Studio Code, .NET Core SDK, and the C# code extension, and created the project using the dotnet new mvc command. I made sure I followed the prompt to add missing assets required to build and debug.

At this point, the tutorials I've read say you can run the application with F5, and they get a template page appear in the browser. However, when I try to run it, it takes me to the Environment dropdown. If I select .NET Core, it takes me to a launch.json file.

I'm new to Visual Studio code, so I'm not sure how best to troubleshoot this, or if there's something I've missed or misunderstood.


Solution

  • In order for Visual Studio Code to run and debug your projects, you must create a launch configuration for each project. (See: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations)

    From the Debug menu choose "Add Configuration..." and choose ".NET Core" for the environment.

    Your launch configuration (launch.json) for a .NET Core MVC project would look something like this:

    {
    // 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}"
        }]
    }
    

    Then you can run and debug your .NET Core MVC web application from Visual Studio Code!