Tag Archives: Visual Studio

How to scaffolding asp.net core MVC View and Controller in Visual Studio Code?

It is natural and conventional for Visual Studio developers to scaffolding asp.net core MVC using the menus and functions provided by Microsoft Visual Studio. But how we scaffolding asp.net core MVC Controller and View inside Visual Studio Code? The VS Code is not offered the menu to scaffolding.

The answer is we need to use command provides by DotNet Core.

To scaffolder a controller, we need to use the command below:
dotnet aspnet-codegenerator controller -name BooksController --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries --readWriteActions

To scaffolder a view, the command is like below:
dotnet aspnet-codegenerator view Index List -m Book -outDir Views\Books --useDefaultLayout --referenceScriptLibraries

If you saw errors like Could not execute because the specified command or file was not found.” during the first time you execute the command, remember you need to install aspnet-codegenerator to run the command successfully.
dotnet tool install -g dotnet-aspnet-codegenerator

Besides that, you need to add scaffolding packages to your project too. Below are some packages need to add, and it depends on your project components.
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

For detail of the command regarding the asp.net core MVC scaffolding, you always can refer to Microsoft dotnet core documentation.
dotnet aspnet-codegenerator

After you have tried it, I believe it is almost exactly same with what has provided by Scaffolding function inside Microsoft Visual Studio. Just take a try and happy coding.