dotnet aspnet-codegenerator

By Rick Anderson

dotnet aspnet-codegenerator - Runs the ASP.NET Core scaffolding engine. dotnet aspnet-codegenerator is only required to scaffold from the command line, it's not needed to use scaffolding with Visual Studio.

Install and update aspnet-codegenerator

Install the .NET SDK.

dotnet aspnet-codegenerator is a global tool that must be installed. The following command installs the latest stable version of the dotnet aspnet-codegenerator tool:

dotnet tool install -g dotnet-aspnet-codegenerator

Note

By default the architecture of the .NET binaries to install represents the currently running OS architecture. To specify a different OS architecture, see dotnet tool install, --arch option. For more information, see GitHub issue dotnet/AspNetCore.Docs #29262.

The following command updates dotnet aspnet-codegenerator to the latest stable version available from the installed .NET Core SDKs:

dotnet tool update -g dotnet-aspnet-codegenerator

Uninstall aspnet-codegenerator

It may be necessary to uninstall the aspnet-codegenerator to resolve problems. For example, if you installed a preview version of aspnet-codegenerator, uninstall it before installing the released version.

The following commands uninstall the dotnet aspnet-codegenerator tool and installs the latest stable version:

dotnet tool uninstall -g dotnet-aspnet-codegenerator
dotnet tool install -g dotnet-aspnet-codegenerator

Synopsis

dotnet aspnet-codegenerator [arguments] [-p|--project] [-n|--nuget-package-dir] [-c|--configuration] [-tfm|--target-framework] [-b|--build-base-path] [--no-build] 
dotnet aspnet-codegenerator [-h|--help]

Description

The dotnet aspnet-codegenerator global command runs the ASP.NET Core code generator and scaffolding engine.

Arguments

generator

The code generator to run. The following generators are available:

Generator Operation
area Scaffolds an Area
controller Scaffolds a controller
identity Scaffolds Identity
razorpage Scaffolds Razor Pages
view Scaffolds a view

Options

-n|--nuget-package-dir

Specifies the NuGet package directory.

-c|--configuration {Debug|Release}

Defines the build configuration. The default value is Debug.

-tfm|--target-framework

Target Framework to use. For example, net46.

-b|--build-base-path

The build base path.

-h|--help

Prints out a short help for the command.

--no-build

Doesn't build the project before running. It also implicitly sets the --no-restore flag.

-p|--project <PATH>

Specifies the path of the project file to run (folder name or full path). If not specified, it defaults to the current directory.

Generator options

The following sections detail the options available for the supported generators:

  • Area
  • Controller
  • Identity
  • Razorpage
  • View

Area options

This tool is intended for ASP.NET Core web projects with controllers and views. It's not intended for Razor Pages apps.

Usage: dotnet aspnet-codegenerator area AreaNameToGenerate

The preceding command generates the following folders:

  • Areas
    • AreaNameToGenerate
      • Controllers
      • Data
      • Models
      • Views

Controller options

The following table lists options for aspnet-codegenerator razorpage, controller and view:

Option Description
--model or -m Model class to use.
--dataContext or -dc The DbContext class to use or the name of the class to generate.
--bootstrapVersion or -b Specifies the bootstrap version. Valid values are 3 or 4. Default is 4. If needed and not present, a wwwroot directory is created that includes the bootstrap files of the specified version.
--referenceScriptLibraries or -scripts Reference script libraries in the generated views. Adds _ValidationScriptsPartial to Edit and Create pages.
--layout or -l Custom Layout page to use.
--useDefaultLayout or -udl Use the default layout for the views.
--force or -f Overwrite existing files.
--relativeFolderPath or -outDir Specify the relative output folder path from project where the file needs to be generated, if not specified, file will be generated in the project folder
--useSqlite or -sqlite Flag to specify if DbContext should use SQLite instead of SQL Server.

The following table lists options unique to aspnet-codegenerator controller:

Option Description
--controllerName or -name Name of the controller.
--useAsyncActions or -async Generate async controller actions.
--noViews or -nv Generate no views.
--restWithNoViews or -api Generate a Controller with REST style API. noViews is assumed and any view related options are ignored.
--readWriteActions or -actions Generate controller with read/write actions without a model.

Use the -h switch for help on the aspnet-codegenerator controller command:

dotnet aspnet-codegenerator controller -h

See Scaffold the movie model for an example of dotnet aspnet-codegenerator controller.

Razorpage

Razor Pages can be individually scaffolded by specifying the name of the new page and the template to use. The supported templates are:

  • Empty
  • Create
  • Edit
  • Delete
  • Details
  • List

For example, the following command uses the Edit template to generate MyEdit.cshtml and MyEdit.cshtml.cs:

dotnet aspnet-codegenerator razorpage MyEdit Edit -m Movie -dc RazorPagesMovieContext -outDir Pages/Movies

Typically, the template and generated file name is not specified, and the following templates are created:

  • Create
  • Edit
  • Delete
  • Details
  • List

The following table lists options for aspnet-codegenerator razorpage, controller and view:

Option Description
--model or -m Model class to use.
--dataContext or -dc The DbContext class to use or the name of the class to generate.
--bootstrapVersion or -b Specifies the bootstrap version. Valid values are 3 or 4. Default is 4. If needed and not present, a wwwroot directory is created that includes the bootstrap files of the specified version.
--referenceScriptLibraries or -scripts Reference script libraries in the generated views. Adds _ValidationScriptsPartial to Edit and Create pages.
--layout or -l Custom Layout page to use.
--useDefaultLayout or -udl Use the default layout for the views.
--force or -f Overwrite existing files.
--relativeFolderPath or -outDir Specify the relative output folder path from project where the file needs to be generated, if not specified, file will be generated in the project folder
--useSqlite or -sqlite Flag to specify if DbContext should use SQLite instead of SQL Server.

The following table lists options unique to aspnet-codegenerator razorpage:

Option Description
--namespaceName or -namespace The name of the namespace to use for the generated PageModel
--partialView or -partial Generate a partial view. Layout options -l and -udl are ignored if this is specified.
--noPageModel or -npm Switch to not generate a PageModel class for Empty template

Use the -h switch for help on the aspnet-codegenerator razorpage command:

dotnet aspnet-codegenerator razorpage -h

See Scaffold the movie model for an example of dotnet aspnet-codegenerator razorpage.

View

Views can be individually scaffolded by specifying the name of the view and the template to use. The supported templates are:

  • Empty
  • Create
  • Edit
  • Delete
  • Details
  • List

For example, the following command uses the Edit template to generate MyEdit.cshtml:

dotnet aspnet-codegenerator view MyEdit Edit -m Movie -dc MovieContext -outDir Views/Movies

The following table lists options for aspnet-codegenerator razorpage, controller and view:

Option Description
--model or -m Model class to use.
--dataContext or -dc The DbContext class to use or the name of the class to generate.
--bootstrapVersion or -b Specifies the bootstrap version. Valid values are 3 or 4. Default is 4. If needed and not present, a wwwroot directory is created that includes the bootstrap files of the specified version.
--referenceScriptLibraries or -scripts Reference script libraries in the generated views. Adds _ValidationScriptsPartial to Edit and Create pages.
--layout or -l Custom Layout page to use.
--useDefaultLayout or -udl Use the default layout for the views.
--force or -f Overwrite existing files.
--relativeFolderPath or -outDir Specify the relative output folder path from project where the file needs to be generated, if not specified, file will be generated in the project folder
--useSqlite or -sqlite Flag to specify if DbContext should use SQLite instead of SQL Server.

The following table lists options unique to aspnet-codegenerator view:

Option Description
--controllerNamespace or -namespace Specify the name of the namespace to use for the generated controller
--partialView or -partial Generate a partial view, other layout options (-l and -udl) are ignored if this is specified

Use the -h switch for help on the aspnet-codegenerator view command:

dotnet aspnet-codegenerator view -h

Identity

See Scaffold Identity