Gustav Ehrenborg fullstack developer

Email symbol LinkedIn logo GitHub logo Instagram logo

Dotnet commands Commands for developing .Net and C#

Wednesday, November 24, 2021
dotnet cli

I have been developing Rails applications for far too many years, but I have finally become a .Net developer. In Rails, one can do pretty much everything in the terminal. Running the server, running tests, scaffolding, running specific tasks, migrations, installing packages and enjoying the power of the Rails console. In .Net, the standard way to do things appears to be by clicking around in Visual Studio. Big dislike.

However, it looks like .Net is becoming more CLI friendly! Here are the commands that I have learnt in my first .Net weeks.

dotnet sdk check # Check what versions of .net that are installed. A "dotnet sdk install x.x" command does not exist. 

dotnet run # restores packages, builds and runs the project. 

dotnet run --project Project.X # same as above, but specific project. The --project flag is available in nearly all commands.  

dotnet run watch # As above, but this will watch for changes and rebuild automatically. This is not hot reloading. 

dotnet clean # Self explanatory

dotnet build # Self explanatory

dotnet test ./path-to-test-project # Runs test! The --project flag does not work here, supply only path.

dotnet restore # Restore nuget packages

dotnet add package PackageName # Installs a package

There is a risk that someone clumsy commits secret environment variables if they are put in appsettings.json. To prevent this, variables can be added using the user-secrets command. They are then put away from your project folder, and added in automatically.

dotnet user-secrets init # Creates an id to identify secrets, this id is added to the .csproj file and should be commited. 

dotnet user-secrets set "MY_API_KEY" "xyz" # Adds xyz as MY_API_KEY

dotnet user-secrets set "SOME_API.MY_API_KEY" "xyz" # As above, but as json object: "SOME_API": { "MY_API_KEY": "xyz"}

cat secrets.json | dotnet user-secrets set # If you have a json file with secrets that you want to add, this works. 

dotnet user-secrets list # Lists the current user secrets

Don't forget to enable tab completion: https://docs.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete