dotnet newdotnet new

本文適用于:✓ .net CORE 1.x sdk .net core 2.x sdkThis article applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK

名稱Name

dotnet new - 根據指定的範本建立新的專案、組態檔或方案。dotnet new - Creates a new project, configuration file, or solution based on the specified template.

概要Synopsis

dotnet new <TEMPLATE> [--dry-run] [--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.

如果 TEMPLATE 值與 [範本] 或 [簡短名稱] 欄中的文字不完全相符,則會對這兩欄執行子字串比對作業。If the TEMPLATE value isn't an exact match on text in the Templates or Short Name column, a substring match is performed on those two columns.

此命令包含預設的範本清單。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.2.100 預先安裝的範本。The following table shows the templates that come pre-installed with the .NET Core SDK 2.2.100. 範本的預設語言會顯示在方括號內。The default language for the template is shown inside the brackets.

範本Templates 簡短名稱Short Name 語言Language TagsTags
主控台應用程式Console Application console [C#], F#, VB[C#], F#, VB 通用/主控台Common/Console
類別庫Class library classlib [C#], F#, VB[C#], F#, VB 通用/程式庫Common/Library
單元測試專案Unit Test Project mstest [C#], F#, VB[C#], F#, VB 測試/MSTestTest/MSTest
NUnit 3 測試專案NUnit 3 Test Project nunit [C#], F#, VB[C#], F#, VB 測試/NUnitTest/NUnit
NUnit 3 測試項目NUnit 3 Test Item nunit-test [C#], F#, VB[C#], F#, VB 測試/NUnitTest/NUnit
xUnit 測試專案xUnit Test Project xunit [C#], F#, VB[C#], F#, VB 測試/XUnitTest/xUnit
Razor 頁面Razor Page page [C#][C#] Web/ASP.NETWeb/ASP.NET
MVC ViewImportsMVC ViewImports viewimports [C#][C#] Web/ASP.NETWeb/ASP.NET
MVC ViewStartMVC ViewStart viewstart [C#][C#] Web/ASP.NETWeb/ASP.NET
空的 ASP.NET CoreASP.NET Core Empty web [C#]、F#[C#], F# Web/空白Web/Empty
ASP.NET Core Web 應用程式 (模型檢視控制器)ASP.NET Core Web App (Model-View-Controller) mvc [C#]、F#[C#], F# Web/MVCWeb/MVC
ASP.NET Core Web 應用程式ASP.NET Core Web App webapprazorwebapp, razor [C#][C#] Web/MVC/Razor 頁面Web/MVC/Razor Pages
ASP.NET Core 與 AngularASP.NET Core with Angular angular [C#][C#] Web/MVC/SPAWeb/MVC/SPA
ASP.NET Core 與 React.jsASP.NET Core with React.js react [C#][C#] Web/MVC/SPAWeb/MVC/SPA
ASP.NET Core 與 React.js 和 ReduxASP.NET Core with React.js and Redux reactredux [C#][C#] Web/MVC/SPAWeb/MVC/SPA
Razor 類別庫Razor Class Library razorclasslib [C#][C#] Web/Razor/程式庫/Razor 類別庫Web/Razor/Library/Razor Class Library
ASP.NET Core Web APIASP.NET Core Web API webapi [C#]、F#[C#], F# Web/WebAPIWeb/WebAPI
global.json 檔案global.json file globaljson 組態Config
NuGet 組態NuGet Config nugetconfig 組態Config
Web 組態Web Config webconfig 組態Config
方案檔Solution File sln 方案Solution

選項Options

--dry-run

若指定的命令會導致建立範本,則顯示執行時會發生的情況摘要。Displays a summary of what would happen if the given command were run if it would result in a template creation.

--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. 預先定義的值為「專案」、「項目」或「其他」。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|NUGET_ID> 值時,會顯示目前已安裝的所有範本組件及其相關聯範本。When excluding the <PATH|NUGET_ID> value, all currently installed template packs and their associated templates are displayed.

注意

若要使用 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

--langVersion <VERSION_NUMBER> - 在建立的專案檔中設定 LangVersion 屬性。--langVersion <VERSION_NUMBER> - Sets the LangVersion property in the created project file. 例如,使用 --langVersion 7.3 可使用 C# 7.3。For example, use --langVersion 7.3 to use C# 7.3. 不支援 F#。Not supported for F#.

--no-restore - 專案建立期間不執行隱含還原。--no-restore - Doesn't execute an implicit restore during project creation.

angular、react、reactreduxangular, react, reactredux

--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.

razorclasslibrazorclasslib

--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.2 建立 .NET Core 類別庫或 netstandard2.0 建立 .NET Standard 類別庫。Values: netcoreapp2.2 to create a .NET Core Class Library or netstandard2.0 to create a .NET Standard Class Library. 預設值為 netstandard2.0The default value is netstandard2.0.

--langVersion <VERSION_NUMBER> - 在建立的專案檔中設定 LangVersion 屬性。--langVersion <VERSION_NUMBER> - Sets the LangVersion property in the created project file. 例如,使用 --langVersion 7.3 可使用 C# 7.3。For example, use --langVersion 7.3 to use C# 7.3. 不支援 F#。Not supported for F#.

--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.

nunitnunit

-f|--framework <FRAMEWORK> - 指定要當成目標的架構-f|--framework <FRAMEWORK> - Specifies the framework to target. 預設值為 netcoreapp2.1The default value is netcoreapp2.1.

-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.

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.

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.

mvc、webappmvc, webapp

-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> - 此專案的登入及註冊原則識別碼。-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> - 此專案的重設密碼原則識別碼。-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> - 此專案的編輯設定檔原則識別碼。-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> - 此專案的用戶端識別碼。--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 識別碼。--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.

--no-https - 專案不需要 HTTPS。--no-https - Project doesn't require HTTPS. app.UseHstsapp.UseHttpsRedirection 並未新增至 Startup.Configureapp.UseHsts and app.UseHttpsRedirection aren't added to Startup.Configure. 此選項僅適用於未使用 IndividualIndividualB2CSingleOrgMultiOrg 時。This option only applies if Individual, IndividualB2C, SingleOrg, or MultiOrg aren't being used.

-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.

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> - 此專案的登入及註冊原則識別碼。-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> - 此專案的用戶端識別碼。--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 識別碼。--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.

--no-https - 專案不需要 HTTPS。--no-https - Project doesn't require HTTPS. app.UseHstsapp.UseHttpsRedirection 並未新增至 Startup.Configureapp.UseHsts and app.UseHttpsRedirection aren't added to Startup.Configure. 此選項僅適用於未使用 IndividualIndividualB2CSingleOrgMultiOrg 時。This option only applies if Individual, IndividualB2C, SingleOrg, or MultiOrg aren't being used.

-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.

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.

範例Examples

藉由指定範本名稱,建立 C# 主控台應用程式專案:Create a C# console application project by specifying the template name:

dotnet new "Console Application"

在目前的目錄中建立 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 project in the current directory with no authentication:

dotnet new mvc -au None

建立新的 xUnit 專案:Create a new xUnit project:

dotnet new xunit

列出 MVC 可用的所有範本:List all templates available for MVC:

dotnet new mvc -l

列出所有符合 we 子字串的範本。List all templates matching the we substring. 找不到完全相符的項目,因此會對 [簡短名稱] 和 [名稱] 兩欄執行子字串比對作業。No exact match is found, so substring matching runs against both the short name and name columns.

dotnet new we -l

嘗試叫用符合 ng 的範本。Attempt to invoke the template matching ng. 如果無法判別單一相符項目,則列出部分相符的範本。If a single match can't be determined, list the templates that are partial matches.

dotnet new ng

安裝適用於 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