Scaffolding: Cannot generate controller from cli

When running dotnet aspnet-codegenerator controller -name AuthorController -async -api -m Author -dc MyDbContext -outDir Controller

I get the following error

Building project ...
Finding the generator 'controller'...
No code generators found with the name 'controller'
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:04.23

Steps to reproduce:

run dotnet aspnet-codegenerator controller -name AuthorController -async -api -m Author -dc MyDbContext -outDir Controller

Here is my csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>latest</LangVersion>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <GenerateDocumentationFile>false</GenerateDocumentationFile>
    <UserSecretsId>xxx</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
  </ItemGroup>

</Project>

Here is my dotnet-tools.json

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "dotnet-ef": {
      "version": "3.1.4",
      "commands": [
        "dotnet-ef"
      ]
    },
    "dotnet-aspnet-codegenerator": {
      "version": "3.1.3",
      "commands": [
        "dotnet-aspnet-codegenerator"
      ]
    }
  }
}

Expected behavior:

Controller to be created

Actual behavior:

Failure

Building project ...
Finding the generator 'controller'...
No code generators found with the name 'controller'
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:04.23

Additional information about the project being scaffolded, such as:

Target framework(s):

netcoreapp3.1

Package version of Microsoft.AspNetCore.App or Microsoft.AspNetCore.All (if applicable):

Not manually selected

Package version of Microsoft.VisualStudio.Web.CodeGeneration.Design - this may be added to your project by scaffolding:

3.1.3

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

I fixed it updating the codegenator package:

  • uninstall codegenerator:

dotnet tool uninstall --global dotnet-aspnet-codegenerator

  • install codegenerator (use your sdk version)

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

  • Restore dotnet restore
  1. Add a global.json to specify your sdk version.

dotnet new globaljson --sdk-version 3.1.300

  1. Change your target framework in csproj file to (specify .NET version)
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  1. Remove the newest version of CodeGeneration installed by default (without version number)

dotnet remove package Microsoft.VisualStudio.Web.CodeGeneration.Design

  1. Add a different, stable version back

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design -v 3.1.0

dotnet restore dotnet build

You can see your SDKs and runtimes installed with.

dotnet info

you can see your packages with

dotnet list package

As for why this happened. No idea, Just downgrading my SDK, .NET core env and the package version worked for me.