Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet-run command not working as expected

I want to run my simple demo of .NET Core project in VS code. When I run dotnet build in my VS Code terminal, it works as expected. But the problem is, in the same project, when I run dotnet run then I get the following error:

Couldn't find a project to run. Ensure a project exists in C:\Users\hp\Desktop\HW-3\FirstTestApp, or pass the path to the project using --project.

This error is totally unexpected for me because this project should be working correctly, but it does not. I am actually not much familiar with VS Code.

enter image description here

And this is my demo project structure:

enter image description here

like image 319
Pritom Sarkar Avatar asked Oct 05 '21 13:10

Pritom Sarkar


People also ask

How do I run a .NET file from command prompt?

The dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code.


1 Answers

dotnet build will build using a solution file if one is present (the *.sln file).

dotnet run however requires a project file (*.csproj).

The easy fix is to just change into the project directory, and then run dotnet run again.

cd C:\Users\hp\Desktop\HW-3\FirstTestApp\FirstTestApp
dotnet run

The documentation for dotnet build and dotnet run sort of makes this clear - the dotnet build [<PROJECT>|<SOLUTION>] command clearly can accept a project or solution file, whereas dotnet run [--project <PATH>] can only accept a project file.

If you are completely new to C# then I will just briefly explain that a .csproj project file is the file that tells the build toolchain how to build the project - it does the same job as a makefile, ant file, pom.xml file, or the main file from any other build system.

The solution file basically just links together several projects into a single solution such that opening the solution file in your editor (VS or VS Code) causes the projects to be loaded as well.

like image 80
RB. Avatar answered Oct 13 '22 17:10

RB.