Creating ASP. NET Core MVC Project Using Visual Studio Code

Partho Sarathi

03 Mar, 2018 · 5 minutes read

  • Share:

This post shows you how to get started with an MVC project using Visual Studio Code – the lightweight IDE by Microsoft.

Pre-requisites

Steps

  1. Open Visual Studio Code

    I am doing this on a brand new Virtual Machine on AWS. The only thing that it has installed on it is Chrome(apart from the Windows Server 2016 defaults ).

    So, as you can see VS Code is asking me to install Gitfor version control. It is not required, but I highly recommend that you install and use it for all your projects.

    Once, I have Git installed, re-opening VS Code looks like this

  2. VS Code has a Terminal built in. Open it by clicking on the Terminal tab at the bottom. Alternatively you could use the shortcut Ctrl+ `

  3. Create a new directory for your project.

    mkdir hello-world-project

  4. Change to the newly created project directory

    cd hello-world-project

  5. Create a new .NET Core project of type mvc

    dotnet new mvc

    You should get an output like this:-

    PS C:\Users\Administrator\hello-world-project> dotnet new mvc
    
        Welcome to .NET Core!
        ---------------------
        Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.
    
        Telemetry
        --------------
        The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The data is collected by Microsoft and shared with the community.
        You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
        You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
        Getting ready...
        The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
        This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
    
        Processing post-creation actions...
        Running 'dotnet restore' on C:\Users\Administrator\hello-world-project\hello-world-project.csproj...
            Restoring packages for C:\Users\Administrator\hello-world-project\hello-world-project.csproj...
            Generating MSBuild file C:\Users\Administrator\hello-world-project\obj\hello-world-project.csproj.nuget.g.props.
            Generating MSBuild file C:\Users\Administrator\hello-world-project\obj\hello-world-project.csproj.nuget.g.targets.
            Restore completed in 7.85 sec for C:\Users\Administrator\hello-world-project\hello-world-project.csproj.
            Restoring packages for C:\Users\Administrator\hello-world-project\hello-world-project.csproj...
            Restore completed in 1.36 sec for C:\Users\Administrator\hello-world-project\hello-world-project.csproj.
    
        Restore succeeded.

    If you get the following error then you don’t have .NET Core SDK installed. Install it from here. Make sure you restart VS Code after installing it because the changes to path settings will only apply on re-launch of VS Code.

    dotnet : The term 'dotnet' is not recognized as the name of a cmdlet, function, script file, or operable program.
  6. Now you can open this project folder using the Open Foldermenu

    Alternatively, you can type code .from the terminal which will open up a new Visual Studio Code Window with the project directory. After this, close the previous VS Code window.

  7. Part of the reason why VSCode is so light is that support for various features have been moved into extensions. In order to support C# projects you will need to install the C# extension for VSCode.

    Opening up any C# code file, will automatically show this suggestion if you do not have the plugin installed.

    Alternatively, you can go to the Extensions Tab ( Ctrl+ Shift+ X) and search for C#and from there install the one from Microsoft

    You will see it being Installed briefly.

    Then hit the Reloadbutton to restart VS Code with the extension enabled.

    VS Code will automatically install C# dependencies such as: OmniSharp& .NET Core Debugger.

    After this you will get another warning that says that the required assets to build & debug this project are missing.

    If I don’t install it and hit F5, VS Code asks me to select the environment to start debugging with.

    So, when you install the missing assets, VS Code creates a .vscode directory with two files – launch.json& tasks.jsonwhich allow you to run/debug your project.

    Now, Hit F5to start debugging your ASP .NET Core application.

    In the Terminal you will get an output like this:-

    > Executing task: C:\Program Files\dotnet\dotnet.exe build C:\Users\Administrator\hello-world-project/hello-world-project.csproj >
    
    Microsoft (R) Build Engine version 15.5.180.51428 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
        Restore completed in 1.51 sec for C:\Users\Administrator\hello-world-project\hello-world-project.csproj.
        Restore completed in 368.92 ms for C:\Users\Administrator\hello-world-project\hello-world-project.csproj.
        hello-world-project -> C:\Users\Administrator\hello-world-project\bin\Debug\netcoreapp2.0\hello-world-project.dll
    
    Build succeeded.
            0 Warning(s)
            0 Error(s)
    
    Time Elapsed 00:00:10.24
    
    Terminal will be reused by tasks, press any key to close it.

    In the Debug Consoleyou will see something like this:-

    info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/favicon.ico'. Physical path: 'C:\Users\Administrator\hello-world-project\wwwroot\favicon.ico'
    Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:Information: Sending file. Request path: '/favicon.ico'. Physical path: 'C:\Users\Administrator\hello-world-project\wwwroot\favicon.ico'
    info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 10.8591ms 200 image/x-icon
    Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 10.8591ms 200 image/x-icon
    The thread 2408 has exited with code 0 (0x0).
    The thread 1224 has exited with code 0 (0x0).

    In the Threadspane you will see a lot of threads running for your Kestrel web server.

    And finally, your ASP .NET Core MVC Application will run inside the browser:-

  • Share:

The cleanest blogging platform


2024 © Maxotek. All rights reserved.