RiddleMeThis
RiddleMeThis

Reputation: 851

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.

Upvotes: 1

Views: 2840

Answers (3)

user22161147
user22161147

Reputation: 1

run command "dotnet run" in your terminal

Upvotes: 0

RiddleMeThis
RiddleMeThis

Reputation: 851

Thanks for the responses. Thanks for the feedback. I've since deleted the test projects, but when I opened the launch.json file, it had some entries, but it was very basic compared to the launch.json file above. For example, it was missing the env and other entries.

"env": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        },

I don't know why it hadn't generated this. I had tried deleting and re-creating the project using the dotnet new mvc command several times, and it was the same each time.

However, now that I have re-visit it a week later, when I run dotnet new mvc, it creates the application with the full launch.json, as seen above, and it runs first time.

Upvotes: 0

rbonestell
rbonestell

Reputation: 439

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!

Upvotes: 3

Related Questions