54

I have created a solution and a webapi project in it's own folder. I have then added the project to the solution.

I would like to be able to run dotnet run without specifying the project by setting a default (Like i would in Visual Studio)

Is this possible or not yet doable with the CLI?

4imble
  • 13,979
  • 15
  • 70
  • 125
  • Maybe alternative is to use docker-compose, just suggestion, I don't know the other way. – Matjaž Oct 10 '17 at 21:31
  • You can make a .bat file that is in the project that you want to run, something like set ASPNETCORE_ENVIRONMENT=Local dotnet run – KenL Oct 10 '17 at 21:38

4 Answers4

39

At the moment, this is not possible with dotnet run.

dotnet run does call msbuild targets to do a restore and build but queries the actually program and arguments to run from a new static evaluation, which means that even if you add custom build logic to a solution (=> the "project" being built), you don't have a chance to run custom msbuild logic to fetch these properties from other projects. (One could still hard-code relative paths to the built executable but this is very cumbersome and not very flexible)

This means the best way would be to create scripts (.bat, .sh) that call the right dotnet run -p my/project command for you without requiring you to do much typing.

mruanova
  • 6,351
  • 6
  • 37
  • 55
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
20

If you are on a *nix system a Makefile can solve all repetitive typing problems.

I generally create a high level Makefile to shorten frequently used commands.

build:
    dotnet build
clean:
    dotnet clean
restore:
    dotnet restore
watch:
    dotnet watch --project src/Main/Main.csproj run
start:
    dotnet run --project src/Main/Main.csproj

The above commands relate to a clean architecture setup where the file structure roughly looks like the following tree.

-- root
|-- src
|    |-- Application
|    |-- Core
|    |-- Infrastructure
|    |-- Main
|-- tests
|    |-- Application.IntegrationTests
|    |-- Core.UnitTests
|    |-- Infrastructure.UnitTests
|-- API.sln
|-- Makefile

With that setup, I can run commands like

make start
nilobarp
  • 3,806
  • 2
  • 29
  • 37
  • this is a really nice way of handling this. +1 to you. I would suggest adding a test: section to the makefile for running unit tests, as well. – tiennen07 Jul 21 '23 at 21:16
6

There appears to be no dotnet configuration file yet - would be nice to see a .dotnetconfig.json or similar, otherewise extending the SLN file to support default project for dotnet command. Following @MartinUllrich line of thinking, assuming you have Node.js installed, simply create a package.json and call npm start. This same pattern could work for other script engines as well.

package.json

{
  "name": "dotnet-run-default-project",
  "private": true,
  "version": "1.0.0", 
  "scripts": {
    "start": "dotnet run -p .\\src\\MyApplication.Web\\"
  }
}

Running Default Project

npm start

Tracker1
  • 19,103
  • 12
  • 80
  • 106
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
-2

An alternative would be to move the project file to the solution directory. And then when dotnet run is executed, it would check that directory for a project to load.

You need not specify --project after that :)

  • 3
    Bad advice. For a small convenience breaking the structure of the project is not desirable. This will also confuse newcomers and complicate CI/CD pipeline implementations. – nilobarp Apr 19 '20 at 23:08