Home Setup Dotnet, ASP.NET app with VS Code on Ubuntu / Linux
Post
Cancel

Setup Dotnet, ASP.NET app with VS Code on Ubuntu / Linux

This article will help you to setup Dotnet/.NET 7, ASP.NET 7 app with VS Code on Ubuntu / Linux

Setup Dotnet / .NET 7

Note: Microsoft offers 2 ways to install dotnet: Scripted and Manual. I will follow the Manual way, so as to have control over installation and since this approach lets you install different versions into separate locations and choose explicitly which one to use by which application

Install Dotnet SDK

  • Install Dotnet SDK binaries from https://dotnet.microsoft.com/en-us/download/dotnet/7.0 & Download Linux tar file

    • Check your architecture version by running:

      1
      
      lscpu
      

      If Architecture: x86_64, this means you are on x64

  • dotnet-sdk-7.0.102-linux-x64.tar.gz file gets downloaded to default ~/Downloads dir.

  • Now go to Downloads dir & create a new dir called .dotnet (we are creating .dotnet dir, since if you directly unzip .tar.gz, it will unzip all its contents to Downloads dir, which we don’t want)

    1
    2
    3
    4
    
    cd ~/Downloads
    mkdir .dotnet
    chmod +x .dotnet
    tar zxvf dotnet-sdk-7.0.102-linux-x64.tar.gz -C .dotnet
    
  • Set DOTNET_ROOT & PATH variables in ~/.bashrc: (vim ~/.bashrc)

    1
    2
    3
    
    # dotnet 7 sdk
    export DOTNET_ROOT=/home/abhi/Downloads/.dotnet
    export PATH=${PATH}:${DOTNET_ROOT}:${DOTNET_ROOT}/tools
    

    Note: I am not sure why we need /tools dir in PATH, as it doesn’t exist! However, this is what Microsoft recommends.

  • Append these variables to .bashrc file, save and now reboot terminal & run either of dotnet commands (dotnet –version or dotnet –info) to test setup:

    1
    
    dotnet --version
    

    successfull output:

    1
    
      7.0.102
    

Setup dotnet auto completion

  • Optional Setup dotnet auto-completion: Add following to ~/.bashrc and reboot terminal:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
      # dotnet auto-completion 
      # bash parameter completion for the dotnet CLI 
      function _dotnet_bash_complete() 
      { 
        local cur="${COMP_WORDS[COMP_CWORD]}" IFS=$'\n' 
    
        local candidates
    
        read -d '' -ra candidates < <(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)
    
        read -d '' -ra COMPREPLY < <(compgen -W "${candidates[*]:-}" -- "$cur")
      }
    
      complete -f -F _dotnet_bash_complete dotnet
    
  • Test auto-completion:

    1
    2
    
    dotnet n (tab-out) -> dotnet new
    dotnet new l (tab-out) -> dotnet new list
    

Create ASP.NET WebApi project

  • Create dir and goto that dir:

    1
    2
    
    mkdir dotnet7-webapi-hello-world
    cd dotnet7-webapi-hello-world
    

    Check what all project types we can create using dotnet cli:

    1
    
    dotnet new list
    
  • Create new dotnet webapi project using dotnet cli:

    1
    
    dotnet new webapi
    
  • At this point, we can use VS Code to open up code and run the project. For now lets continue using dotnet cli. Later in this article, we will install VS Code and open up this app.

    1
    
    dotnet build
    
  • Run the application:

    1
    
    dotnet run
    

Checking url / port number mentioned in the terminal, as output of above dotnet run cmd

To stop the application, press Ctrl+C in the terminal

Setup VS Code

  • Download VS Code (free) from https://code.visualstudio.com/

  • In your default ~/Downloads folder, you will see code_1.74.3-1673284829_amd64.deb

  • Double click to open in Software Install and go ahead and install VS Code

  • Open up VS code either from menu or from terminal by navigating to your above project dir:

    1
    
    code . 
    

Install C# extension

  • Install C# extension from ‘Extensions’ panel (Ctrl+Shift+X) on left hand side

Enable C# debugging

  • Enable C# debugging by opening up any .cs file and then you will get a Notification from C# extension: Required assets to build and debug are missing from ‘project-name’. Add them? OR

  • Enable C# debugging ‘Run and Debug’ panel (Ctrl+Shift+X) on left hand side: enable vscode debugging 1

    • This will generate .vscode folder with 2 files, which allows C# extension to provide debugger in VS Code: enable vscode debugging 2

    • Open up a .cs file, add a Breakpoint and Start Debugging from Run menu or press F5: vscode debugging breakpoint

    • To stop debugging press Shift+F5 or from debug options bar, click on red stop button.

Note: dotnet/VS Code IDE versions may vary in future or you might be using a different distro of Linux, but this guide should still be valid.

This post is licensed under CC BY 4.0 by the author.

Setup JDK, maven, Java Spring Boot app with IntelliJ Idea on Ubuntu / Linux

AWS re:Invent 2022 Keynote by Peter DeSantis

Comments powered by Disqus.