Search code examples
c#asp.net-coreasp.net-core-mvc.net-coreasp.net-core-cli

Is it possible to scaffold my controller using the .NET CLI?


I'm following the ASP.NET Core tutorials at https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model. I'm up to the Scaffolding a Controller section and I'm trying to create a controller from my model/DB context. However, when I follow the instructions as described in the guide, I receive this error:

enter image description here

I tried restoring the NuGet packages and creating a new project, but I'm still getting the same error. Is there a way to scaffold the controller using the .NET CLI? I've never seen one before, so I don't know how to write one up myself.

Thanks!

edit: I just tried @Prafull8495's answer and modified my project file as he instructed. The above error is no longer popping up, but now I'm hitting something else:

enter image description here


Solution

  • For a csproj based ASP.NET Core project, you would need to make sure the below references are present (Targeting 1.0.x version of ASP.NET Core)

    <ItemGroup>    
      <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.0.0-msbuild3-final" />
      <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
    </ItemGroup>
    

    (For projects targeting 1.1.x version of ASP.NET Core)

    <ItemGroup>    
      <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0-msbuild3-final" />
      <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
    </ItemGroup>
    

    Note that the version of Microsoft.VisualStudio.Web.CodeGeneration.Tools package remains the same in both the cases.

    Also make sure that the below package is not referenced in the project as a PackageReference

    • Microsoft.VisualStudio.Web.CodeGenerators.Mvc
    • Microsoft.VisualStudio.Web.CodeGeneration.Tools