dotnet newdotnet new

此主題適用於:✓ .NET Core SDK 1.x .NET Core SDK 2.0This topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x

名稱Name

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
Razor 頁面Razor page page [C#][C#]
MVC ViewImportsMVC ViewImports viewimports [C#][C#]
MVC ViewStartMVC ViewStart viewstart [C#][C#]
空的 ASP.NET CoreASP.NET Core empty web [C#]、F#[C#], F#
ASP.NET Core Web 應用程式 (模型檢視控制器)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#]
ASP.NET Core 與 AngularASP.NET Core with Angular angular [C#][C#]
ASP.NET Core 與 React.jsASP.NET Core with React.js react [C#][C#]
ASP.NET Core 與 React.js 和 ReduxASP.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. 預先定義的值為「專案」、「項目」或「其他」。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:

主控台, 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> - 此專案的登入及註冊原則識別碼。-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.

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

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

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

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