dotnet newdotnet new

本主题适用于:✓ .NET Core SDK 1.x .NET Core SDK 2.xThis topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x

nameName

dotnet new - 根据指定的模板,创建新的项目、配置文件或解决方案。dotnet new - Creates a new project, configuration file, or solution based on the specified template.

摘要Synopsis

dotnet new <TEMPLATE> [--force] [-i|--install] [-lang|--language] [-n|--name] [--nuget-source] [-o|--output]
    [-u|--uninstall] [Template options]
dotnet new <TEMPLATE> [-l|--list] [--type]
dotnet new [-h|--help]

说明Description

dotnet new 命令为初始化有效的 .NET Core 项目提供了便捷方法。The dotnet new command provides a convenient way to initialize a valid .NET Core project.

命令调用模板引擎,以根据指定的模板和选项在磁盘上创建项目。The command calls the template engine to create the artifacts on disk based on the specified template and options.

自变量Arguments

TEMPLATE

调用命令时要实例化的模板。The template to instantiate when the command is invoked. 每个模板可能具有可传递的特定选项。Each template might have specific options you can pass. 有关详细信息,请参阅模板选项For more information, see Template options.

此命令包含默认的模板列表。The command contains a default list of templates. 使用 dotnet new -l 获取可用模板的列表。Use dotnet new -l to obtain a list of the available templates. 下表列出了随 .NET Core SDK 2.1.300 一起预安装的模板。The following table shows the templates that come pre-installed with the .NET Core SDK 2.1.300. 模板的默认语言显示在括号内。The default language for the template is shown inside the brackets.

模板描述Template description 模板名称Template name 语言Languages
控制台应用程序Console application console [C#]、F#、VB[C#], F#, VB
类库Class library classlib [C#]、F#、VB[C#], F#, VB
单元测试项目Unit test project mstest [C#]、F#、VB[C#], F#, VB
xUnit 测试项目xUnit test project xunit [C#]、F#、VB[C#], F#, VB
NUnit 试项目NUnit test project nunit [C#]、F#、VB[C#], F#, VB
Razor 页Razor page page [C#][C#]
MVC ViewImportsMVC ViewImports viewimports [C#][C#]
MVC ViewStartMVC ViewStart viewstart [C#][C#]
ASP.NET Core 空ASP.NET Core empty web [C#],F#[C#], F#
ASP.NET Core Web 应用程序 (Model-View-Controller)ASP.NET Core Web App (Model-View-Controller) mvc [C#],F#[C#], F#
ASP.NET Core Web 应用程序ASP.NET Core Web App razorwebapprazor, webapp [C#][C#]
含 Angular 的 ASP.NET CoreASP.NET Core with Angular angular [C#][C#]
含 React.js 的 ASP.NET CoreASP.NET Core with React.js react [C#][C#]
含 React.js 和 Redux 的 ASP.NET CoreASP.NET Core with React.js and Redux reactredux [C#][C#]
ASP.NET Core Web APIASP.NET Core Web API webapi [C#],F#[C#], F#
Razor 类库Razor class library razorclasslib [C#][C#]
global.json 文件global.json file globaljson
NuGet 配置NuGet config nugetconfig
Web 配置Web config webconfig
解决方案文件Solution file sln

选项Options

--force

强制生成内容,即使会更改现有文件,也不例外。Forces content to be generated even if it would change existing files. 输出目录已包含一个项目时,可能需要此操作。This is required when the output directory already contains a project.

-h|--help

打印命令帮助。Prints out help for the command. 可针对 dotnet new 命令本身或任何模板(如 dotnet new mvc --help)调用它。It can be invoked for the dotnet new command itself or for any template, such as dotnet new mvc --help.

-i|--install <PATH|NUGET_ID>

从提供的 PATHNUGET_ID 安装源或模板包。Installs a source or template pack from the PATH or NUGET_ID provided. 若要安装模板包的预发布版本,需要以 <package-name>::<package-version> 格式指定该版本。If you want to install a prerelease version of a template package, you need to specify the version in the format of <package-name>::<package-version>. 默认情况下,dotnet new 为该版本传递 *,表示最后一个稳定的包版本。By default, dotnet new passes * for the version, which represents the last stable package version. 请在示例部分查看示例。See an example at the Examples section.

若要了解如何创建自定义模板,请参阅 dotnet new 自定义模板For information on creating custom templates, see Custom templates for dotnet new.

-l|--list

列出包含指定名称的模板。Lists templates containing the specified name. 如果针对 dotnet new 命令调用,则它列出可能对给定的目录可用的模板。If invoked for the dotnet new command, it lists the possible templates available for the given directory. 例如,如果该目录已包含一个项目,则它不会列出所有项目模板。For example if the directory already contains a project, it doesn't list all project templates.

-lang|--language {C#|F#|VB}

要创建的模板的语言。The language of the template to create. 接受的语言因模板而异(请参阅参数部分中的默认值)。The language accepted varies by the template (see defaults in the arguments section). 对于某些模板无效。Not valid for some templates.

备注

某些 shell 将 # 解释为特殊字符。Some shells interpret # as a special character. 在这些情况下,需要括住语言参数值,如 dotnet new console -lang "F#"In those cases, you need to enclose the language parameter value, such as dotnet new console -lang "F#".

-n|--name <OUTPUT_NAME>

所创建的输出的名称。The name for the created output. 如果未指定名称,使用的是当前目录的名称。If no name is specified, the name of the current directory is used.

--nuget-source

指定在安装期间要使用的 NuGet 源。Specifies a NuGet source to use during install.

-o|--output <OUTPUT_DIRECTORY>

用于放置生成的输出的位置。Location to place the generated output. 默认为当前目录。The default is the current directory.

--type

根据可用类型筛选模板。Filters templates based on available types. 预定义值为“project”、“item”或“other”。Predefined values are "project", "item" or "other".

-u|--uninstall <PATH|NUGET_ID>

从提供的 PATHNUGET_ID 卸载源或模板包。Uninstalls a source or template pack at the PATH or NUGET_ID provided.

备注

若要使用 PATH 卸载模板,需要完全限定路径。To uninstall a template using a PATH, you need to fully qualify the path. 例如,C:/Users/<USER>/Documents/Templates/GarciaSoftware.ConsoleTemplate.CSharp 有效,但是包含文件夹中的 ./GarciaSoftware.ConsoleTemplate.CSharp 无效。For example, C:/Users/<USER>/Documents/Templates/GarciaSoftware.ConsoleTemplate.CSharp will work, but ./GarciaSoftware.ConsoleTemplate.CSharp from the containing folder will not. 此外,模板路径中不要包含最后的终止目录斜杠。Additionally, do not include a final terminating directory slash on your template path.

模板选项Template options

每个项目模板都可能有附加选项。Each project template may have additional options available. 核心模板有以下附加选项:The core templates have the following additional options:

console、angular、react、reactredux、razorclasslibconsole, angular, react, reactredux, razorclasslib

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

classlibclasslib

-f|--framework <FRAMEWORK> - 指定目标框架-f|--framework <FRAMEWORK> - Specifies the framework to target. 值:netcoreapp2.0(要创建 .NET Core 类库的话)或 netstandard2.0(要创建 .NET Standard 类库的话)。Values: netcoreapp2.0 to create a .NET Core Class Library or netstandard2.0 to create a .NET Standard Class Library. 默认值为 netstandard2.0The default value is netstandard2.0.

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

mstest、xunitmstest, xunit

-p|--enable-pack - 允许使用 dotnet pack 为项目打包。-p|--enable-pack - Enables packaging for the project using dotnet pack.

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

globaljsonglobaljson

--sdk-version <VERSION_NUMBER> - 指定要在 global.json 文件中使用的 .NET Core SDK 版本。--sdk-version <VERSION_NUMBER> - Specifies the version of the .NET Core SDK to use in the global.json file.

webweb

--exclude-launch-settings - 从生成的模板中排除 launchSettings.json。--exclude-launch-settings - Exclude launchSettings.json from the generated template.

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

--no-https - 项目不需要 HTTPS。--no-https - Project doesn't require HTTPS. 此选项仅适用于未使用 IndividualAuthOrganizationalAuth 的情况。This option only applies if IndividualAuth or OrganizationalAuth are not being used.

webapiwebapi

-au|--auth <AUTHENTICATION_TYPE> - 要使用的身份验证类型。-au|--auth <AUTHENTICATION_TYPE> - The type of authentication to use. 可能的值为:The possible values are:

  • None - 不进行身份验证(默认)。None - No authentication (Default).
  • IndividualB2C - 使用 Azure AD B2C 进行个人身份验证。IndividualB2C - Individual authentication with Azure AD B2C.
  • SingleOrg - 对一个租户进行组织身份验证。SingleOrg - Organizational authentication for a single tenant.
  • Windows - Windows 身份验证。Windows - Windows authentication.

--aad-b2c-instance <INSTANCE> - 要连接到的 Azure Active Directory B2C 实例。--aad-b2c-instance <INSTANCE> - The Azure Active Directory B2C instance to connect to. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication. 默认值为 https://login.microsoftonline.com/tfp/The default value is https://login.microsoftonline.com/tfp/.

-ssp|--susi-policy-id <ID> - 此项目的登录和注册策略 ID。-ssp|--susi-policy-id <ID> - The sign-in and sign-up policy ID for this project. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication.

--aad-instance <INSTANCE> - 要连接到的 Azure Active Directory 实例。--aad-instance <INSTANCE> - The Azure Active Directory instance to connect to. SingleOrg 身份验证结合使用。Use with SingleOrg authentication. 默认值为 https://login.microsoftonline.com/The default value is https://login.microsoftonline.com/.

--client-id <ID> - 此项目的客户端 ID。--client-id <ID> - The Client ID for this project. IndividualB2CSingleOrg 身份验证结合使用。Use with IndividualB2C or SingleOrg authentication. 默认值为 11111111-1111-1111-11111111111111111The default value is 11111111-1111-1111-11111111111111111.

--domain <DOMAIN> - 目录租户的域。--domain <DOMAIN> - The domain for the directory tenant. SingleOrgIndividualB2C 身份验证结合使用。Use with SingleOrg or IndividualB2C authentication. 默认值为 qualified.domain.nameThe default value is qualified.domain.name.

--tenant-id <ID> - 要连接到的目录的 TenantId ID。--tenant-id <ID> - The TenantId ID of the directory to connect to. SingleOrg 身份验证结合使用。Use with SingleOrg authentication. 默认值为 22222222-2222-2222-2222-222222222222The default value is 22222222-2222-2222-2222-222222222222.

-r|--org-read-access - 允许此应用程序对目录进行读取访问。-r|--org-read-access - Allows this application read-access to the directory. 仅适用于 SingleOrgMultiOrg 身份验证。Only applies to SingleOrg or MultiOrg authentication.

--exclude-launch-settings - 从生成的模板中排除 launchSettings.json。--exclude-launch-settings - Exclude launchSettings.json from the generated template.

-uld|--use-local-db - 指定应使用 LocalDB,而不使用 SQLite。-uld|--use-local-db - Specifies LocalDB should be used instead of SQLite. 仅适用于 IndividualIndividualB2C 身份验证。Only applies to Individual or IndividualB2C authentication.

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

--no-https - 项目不需要 HTTPS。--no-https - Project doesn't require HTTPS. app.UseHstsapp.UseHttpsRedirection 未添加到 Startup.Configure 中。app.UseHsts and app.UseHttpsRedirection aren't added to Startup.Configure. 此选项仅适用于未使用 IndividualIndividualB2C``SingleOrgMultiOrg 的情况。This option only applies if Individual, IndividualB2C, SingleOrg, or MultiOrg aren't being used.

mvc、razormvc, razor

-au|--auth <AUTHENTICATION_TYPE> - 要使用的身份验证类型。-au|--auth <AUTHENTICATION_TYPE> - The type of authentication to use. 可能的值为:The possible values are:

  • None - 不进行身份验证(默认)。None - No authentication (Default).
  • Individual - 个人身份验证。Individual - Individual authentication.
  • IndividualB2C - 使用 Azure AD B2C 进行个人身份验证。IndividualB2C - Individual authentication with Azure AD B2C.
  • SingleOrg - 对一个租户进行组织身份验证。SingleOrg - Organizational authentication for a single tenant.
  • MultiOrg - 对多个租户进行组织身份验证。MultiOrg - Organizational authentication for multiple tenants.
  • Windows - Windows 身份验证。Windows - Windows authentication.

--aad-b2c-instance <INSTANCE> - 要连接到的 Azure Active Directory B2C 实例。--aad-b2c-instance <INSTANCE> - The Azure Active Directory B2C instance to connect to. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication. 默认值为 https://login.microsoftonline.com/tfp/The default value is https://login.microsoftonline.com/tfp/.

-ssp|--susi-policy-id <ID> - 此项目的登录和注册策略 ID。-ssp|--susi-policy-id <ID> - The sign-in and sign-up policy ID for this project. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication.

-rp|--reset-password-policy-id <ID> - 此项目的重置密码策略 ID。-rp|--reset-password-policy-id <ID> - The reset password policy ID for this project. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication.

-ep|--edit-profile-policy-id <ID> - 此项目的编辑配置文件策略 ID。-ep|--edit-profile-policy-id <ID> - The edit profile policy ID for this project. IndividualB2C 身份验证结合使用。Use with IndividualB2C authentication.

--aad-instance <INSTANCE> - 要连接到的 Azure Active Directory 实例。--aad-instance <INSTANCE> - The Azure Active Directory instance to connect to. SingleOrgMultiOrg 身份验证结合使用。Use with SingleOrg or MultiOrg authentication. 默认值为 https://login.microsoftonline.com/The default value is https://login.microsoftonline.com/.

--client-id <ID> - 此项目的客户端 ID。--client-id <ID> - The Client ID for this project. IndividualB2CSingleOrgMultiOrg 身份验证结合使用。Use with IndividualB2C, SingleOrg, or MultiOrg authentication. 默认值为 11111111-1111-1111-11111111111111111The default value is 11111111-1111-1111-11111111111111111.

--domain <DOMAIN> - 目录租户的域。--domain <DOMAIN> - The domain for the directory tenant. SingleOrgIndividualB2C 身份验证结合使用。Use with SingleOrg or IndividualB2C authentication. 默认值为 qualified.domain.nameThe default value is qualified.domain.name.

--tenant-id <ID> - 要连接到的目录的 TenantId ID。--tenant-id <ID> - The TenantId ID of the directory to connect to. SingleOrg 身份验证结合使用。Use with SingleOrg authentication. 默认值为 22222222-2222-2222-2222-222222222222The default value is 22222222-2222-2222-2222-222222222222.

--callback-path <PATH> - 重定向 URI 的应用程序基路径中的请求路径。--callback-path <PATH> - The request path within the application's base path of the redirect URI. SingleOrgIndividualB2C 身份验证结合使用。Use with SingleOrg or IndividualB2C authentication. 默认值为 /signin-oidcThe default value is /signin-oidc.

-r|--org-read-access - 允许此应用程序对目录进行读取访问。-r|--org-read-access - Allows this application read-access to the directory. 仅适用于 SingleOrgMultiOrg 身份验证。Only applies to SingleOrg or MultiOrg authentication.

--exclude-launch-settings - 从生成的模板中排除 launchSettings.json。--exclude-launch-settings - Exclude launchSettings.json from the generated template.

--use-browserlink - 在项目中添加 BrowserLink。--use-browserlink - Includes BrowserLink in the project.

-uld|--use-local-db - 指定应使用 LocalDB,而不使用 SQLite。-uld|--use-local-db - Specifies LocalDB should be used instead of SQLite. 仅适用于 IndividualIndividualB2C 身份验证。Only applies to Individual or IndividualB2C authentication.

--no-restore - 在项目创建期间不执行隐式还原。--no-restore - Doesn't execute an implicit restore during project creation.

--no-https - 项目不需要 HTTPS。--no-https - Project doesn't require HTTPS. app.UseHstsapp.UseHttpsRedirection 未添加到 Startup.Configure 中。app.UseHsts and app.UseHttpsRedirection aren't added to Startup.Configure. 此选项仅适用于未使用 IndividualIndividualB2C``SingleOrgMultiOrg 的情况。This option only applies if Individual, IndividualB2C, SingleOrg, or MultiOrg aren't being used.

pagepage

-na|--namespace <NAMESPACE_NAME> - 生成的代码的命名空间。-na|--namespace <NAMESPACE_NAME>- Namespace for the generated code. 默认值为 MyApp.NamespaceThe default value is MyApp.Namespace.

-np|--no-pagemodel - 创建不含 PageModel 的页。-np|--no-pagemodel - Creates the page without a PageModel.

viewimportsviewimports

-na|--namespace <NAMESPACE_NAME> - 生成的代码的命名空间。-na|--namespace <NAMESPACE_NAME>- Namespace for the generated code. 默认值为 MyApp.NamespaceThe default value is MyApp.Namespace.

示例Examples

在当前目录中创建 F# 控制台应用程序项目:Create an F# console application project in the current directory:

dotnet new console -lang F#

在指定目录中创建 .NET Standard 类库项目(仅适用于 .NET Core SDK 2.0 或更高版本):Create a .NET Standard class library project in the specified directory (available only with .NET Core SDK 2.0 or later versions):

dotnet new classlib -lang VB -o MyLibrary

在当前目录中新建没有设置身份验证的 ASP.NET Core C# MVC 应用程序项目:Create a new ASP.NET Core C# MVC application project in the current directory with no authentication:

dotnet new mvc -au None

创建新的 xUnit 应用程序:Create a new xUnit application:

dotnet new xunit

列出适用于 MVC 的所有模板:List all templates available for MVC:

dotnet new mvc -l

安装适用于 ASP.NET Core 的单页应用程序模板 2.0 版(命令选项仅可用于 .NET Core SDK 1.1 及更高版本):Install version 2.0 of the Single Page Application templates for ASP.NET Core (command option available for .NET Core SDK 1.1 and later versions only):

dotnet new -i Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0

在当前目录中创建 global.json,将 SDK 版本设为 2.0.0(仅适用于 .NET Core SDK 2.0 或更高版本):Create a global.json in the current directory setting the SDK version to 2.0.0 (available only with .NET Core SDK 2.0 or later versions):

dotnet new globaljson --sdk-version 2.0.0

请参阅See also