63

When trying to add a Controller in an ASP.NET Core project using Visual Studio 15 Enterprise with Update 3, I get the error below:

"The was an error running the selected code generator: No executables found matching command 'dotnet-aspnet-codegenerator'"

13 Answers 13

149

If you're using csproj (Visual Studio 2017) instead of project.json, then you need to add the following to your csproj file:

    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
    </ItemGroup>
10
  • Currently - Version="1.0.0"
    – CountZero
    Commented Apr 12, 2017 at 21:07
  • @CountZero , I have ` Version="1.0.1"` how best to check the latest version number? And can I install this Reference using dotnet cli itself? When I tried dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Tools it created an <PackageReference>, but I needed a <DotNetCliToolReference> as johnnycardy answered here... One advantage of dotnet add package is that (it seems?) it will find and add an element with the latest version attribute, so I just manually converted from <PackageReference> -> <DotNetCliToolReference> Commented May 24, 2017 at 21:33
  • Looks like 1.0.1 is the latest unless you're using a preview. nuget.org/packages/…
    – CountZero
    Commented May 24, 2017 at 21:44
  • What if this line is already there in the csproj file and we are still getting the same error? I am facing this error in VS 2017 (release and latest update installed as of date) Commented Jul 8, 2017 at 21:05
  • 1
    @SteveJohnson Thank you. Lol I figured out that that was the problem (everything is now 2.0.0), and solved the codegenerator still not being found by using the dos cmd prompt for the cmd. Details here for future readers: stackoverflow.com/questions/47765196/… Commented Dec 13, 2017 at 3:54
8

If you are using Mac (OS X) or any supported distribution of Linux, you have to run:

dotnet tool install --global dotnet-aspnet-codegenerator --version 2.2.3

Additionally, on Mac I added to my .zshrc (or bash equivalent)

export PATH=$HOME/.dotnet/tools:$PATH

And I had to make sure to restart Terminal.

1
  • 1
    Adding .dotnet/tools to my PATH in Mint 19 solved my problem. Commented Apr 25, 2019 at 7:43
7

For the latest version, in project.json add the following under dependencies:

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.1.0-preview4-final",
  "type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
  "type": "build",
  "version": "1.1.0-preview4-final"
}

and the following under tools:

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.1.0-preview4-final",
  "imports": [
    "portable-net45+win8"
  ]
}
1
  • There is no "project.json" file for the newest asp.net core version.
    – user7739271
    Commented Aug 7, 2017 at 8:13
7

A more robust answer than copying version numbers into your configuration file is to use NuGet to ensure that the packages are added to your project.

Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.

First, get everything up to date. Choose the Updates tab. Check the box for Update All and run this a few times. Don't be surprised if some stuff downgrades the first couple of times you run the upgrade. Some dependencies seem to have to be handled sequentially. It took me about 5 upgrades to get everything up to date.

Then, in the browse tab, search for CodeGeneration.Tools. Install it. Do the same for CodeGenerators.Mvc. As you find additional error messages, you should be able to find any missing packages in NuGet.

1
  • 5
    This is what I see when I try to install CodeGeneration.Tools to my project: Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.1' has a package type 'DotnetCliTool' that is not supported by project
    – Piotrek
    Commented May 11, 2017 at 20:16
4

In dotnet core 2.1.1 you'd expect that the situation has changed and you may not need to add much. I'm sorry to annoy you but the situation is same and all you need to do now is update your version of the tool or package you wish to use.

 <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>

This worked for me. I hope it works for eveyone else that gets stuck here. Note that the key reference is DotNetCliToolReference not PackageReference

3

Add the following to your project.json:

Under dependencies:

"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
    "version": "1.0.0-preview2-final",
    "type": "build"
}

Under tools:

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
    "version": "1.0.0-preview2-final",
    "imports": [
        "portable-net45+win8"
    ]
}
  • Version number may change depending on which version of .NET Core you're using in your project
  • You may get another error about Microsoft.DotNet.InternalAbstractions missing, in which case you'll need to get from NuGet
  • Make sure "Microsoft.VisualStudio.Web.CodeGeneration.Tools" version in dependencies matches "Microsoft.VisualStudio.Web.CodeGeneration.Tools" version in tools
1
  • Thank you for posting your solution and specifically describing variation of version # for different users etc. It saved me tons of hours.
    – nam
    Commented Jan 5, 2017 at 21:05
2
  1. I had to add the following to my CSProj file:

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild3-final" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
</ItemGroup>

  1. After adding that I installed CodeGenerators.Mvc with nuget package manager.

  2. I was still getting an error saying it can't find some file in the MCD folder so I had to copy and paste the entire bin\Debug\netcoreapp1.1 folder into bin\MCD\Debug\netcoreapp1.1

I ran the scaffolding and it worked!

2

I encountered the same issue in Visual Studio Mac Community Edition 2017. Prior to running the scaffold command from the project directory, make sure the directory has the Program.cs, Startup.cs and .csproj files. if not, then run the command ls-al and then cd into the project directory which would be inside your current project directory and then execute the scaffold command. An obvious mistake many overlook.

1
  • 1
    This was the solution for me (and you're right very obvious! LOL). I was using JetBrains Rider (which has a solution and separate sub folder for project) and following instructions for vscode which doesn't. Once I jumped into the project folder not the solution it worked. Rider defaults it's Terminal to the solution. :)
    – GazB
    Commented Feb 18, 2018 at 15:36
1

Just add tag 'DotNetCliToolReference ' and package code design on .csproj and execute code-generate command on root solution. Thats worked to me.

.csproj <ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" /> </ItemGroup>

<ItemGroup> ... <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" /> </ItemGroup>

Command PS C:\Users\miche\projetos\asp_net_core\crud> dotnet aspnet-codegenerator controller -name ProdutosController -m Produto -dc AppDataContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

Don't forget to build and restore solution after add package ;)

0
0

For VS 2015, in project.json file -

under dependencies add -

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.0.0-preview2-final",
  "imports": [
    "portable-net45+win8"
  ]
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final"

then under tools add-

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.0.0-preview2-final",
  "imports": [
    "portable-net45+win8"
  ]
} 
0

In Visual Studio Code change your yourproject.csproj

<pre>
<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.6" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" version= "2.1.0-preview1-final" />
    <PackageReference Include="Microsoft.Extensions.SecretManager.Tools" version= "2.0.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
  </ItemGroup>
  <ItemGroup>
  <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" version="2.1.0-preview1-final" />
  </ItemGroup>`enter code here`
</pre>
1
  • pre tag is not there Commented Dec 21, 2018 at 4:44
0

I just typed in Linux:

~/.dotnet/tools/dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries

So, I did not start with 'dotnet' (my current version: 2.2.300)

I do not like this solution, but it worked.

0

On Windows 10

In my case the installer added the wrong path to the Path environment variable.
The path added was pointing to a non existing folder under Programs. It needs to point to dotnet-aspnet-codegenerator.exe.
For me the correct path was in my user folder: ~\.dotnet\tools

You can check if the correct path was added by running: echo $env:Path
If the path is missing or incorrect you just need to add the correct path to the Path system environment variable.

You might be able to test this by using PowerShell to set your local variable: $env:Path += ";C:\Users\<YOUR_NAME_HERE>\.dotnet\tools"
But I haven't tried this.


To fix it globally

  1. Start typing Environment in the windows search and you should see the Control panel option to Edit system environment variables.
  2. Click the Environment Variables... button in the lower right corner.
  3. Under System variables find and select the Path variable, then click Edit.
  4. Check if the path to dotnet-aspnet-codegenerator.exe is there and if not click New and add it.
  5. Restart your computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.