创建新项目时如何更改 dotnet new 模板的名称?

Ful*_*ime 4 .net asp.net visual-studio .net-core

我创建了一个dotnet与 一起使用的模板dotnet new。从该模板创建新项目时,它工作正常,但程序集和命名空间名称仍然是“Template”,而不是新项目。

--name我尝试过创建一个新项目并使用flag和flag手动设置名称--output,但无济于事。

我怀疑我可以在某个地方使用一个变量,也许在 template.json 文件中?

谢谢,

小智 6

您必须设置模板,然后使用选项参数(例如-n param)并给出名称。例如,

dotnet new web -n myBestName
Run Code Online (Sandbox Code Playgroud)

您可以点击此处查看有关 cli 源的更多信息


sat*_*esh 5

你需要sourceName在你的template.json文件中设置。

您可以参考此页面了解每个字段的用途。

"sourceName": {
  "description": "The name in the source tree to replace with the name the user specifies",
  "type": "string"
},
Run Code Online (Sandbox Code Playgroud)

http://json.schemastore.org/template

文件的基本示例template.json如下所示。注意sourceName

{
    "$schema": "http://json.schemastore.org/template",
    "author": "Me",
    "classifications": [ "web api", "asp.net core", "C#" ],
    "identity": "WebApi.Template",
    "name": "WebApi.Template",
    "shortName": "WebApiTemplate",
    "tags": {
      "language": "C#",
      "type": "project"
    },
    "sourceName": "WebApi.Template",
    "preferNameDirectory" : true
  }
Run Code Online (Sandbox Code Playgroud)

如果您使用template.json与上面类似的方式,您可以在 cli 命令中指定组件名称,或者在以组件名称命名的文件夹中dotnet new YourTemplateName -n YourComponentName运行。dotnet new YourTemplateName