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.

This article applies to .NET Core 2.1 SDK and later.

Installing aspnet-codegenerator

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

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

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 controller and razorpage:

Option Description
--model or -m Model class to use.
--dataContext or -dc The DbContext class to use.
--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 The relative output folder path from project where the file are generated. If not specified, files are generated in the project folder.

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 and controller:

Option Description
--model or -m Model class to use.
--dataContext or -dc The DbContext class to use.
--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 The relative output folder path from project where the file are generated. If not specified, files are generated in the project folder.

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.

Identity

See Scaffold Identity