使用VSCode开发.NET Core MVC项目,在Solution Explorer的项目目录文件夹右键创建新文件时,会在输入框提示选择模板创建文件,输入文件名点Enter回车后,文件没有创建成功,而是报错了。

Error running command csdevkit.addNewFile: Failed with ExitCode(2097158)

Details

No templates found matching: 'class'.

To list installed templates, run:

   dotnet new --list

To search for the templates on NuGet.org, run:

   dotnet new class --search

. This is likely caused by the extension that contributes csdevkit.addNewFile.

解决方法:

根据错误提示,命令行输入

dotnet new --list

输出如下结果:

搜索出了已经安装的模板。

These templates matched your input: 

Template Name                                 Short Name           Language    Tags

--------------------------------------------  -------------------  ----------  -------------------------------------

ASP.NET Core Empty                            web                  [C#],F#     Web/Empty

ASP.NET Core gRPC Service                     grpc                 [C#]        Web/gRPC

ASP.NET Core Web API                          webapi               [C#],F#     Web/WebAPI

ASP.NET Core Web App                          webapp,razor         [C#]        Web/MVC/Razor Pages

...

为省略篇幅,略去部分。看上去是因为没有安装Class的模板。

接下去输入下面的命令:

dotnet new class --search

输出如下结果:

Searching for the templates...

Matches from template source: NuGet.org

These templates matched your input: 'class'

 

Template Name                                  Short Name                            Author                             Language  Package                                    Downloads

---------------------------------------------  ------------------------------------  ---------------------------------  --------  -----------------------------------------  ---------

Backlang Class                                 backclass                             Me                                 Backlang  Backlang.Templates                               21k 

Basic Class Library that can be made into ...  DropBear.CLT                          DropBear aka Terrence Kuchel       [C#]      DropBear.Templates.ClassLibrary                  <1k 

Benjamin Michaelis's Class Library for NuGet   benjaminmichaelis.nuget               BenjaminMichaelis                  [C#]      BenjaminMichaelis.Dotnet.Templates               <1k 

Blazor Hybrid Application using WPF with s...  blazorwpfrcl                          Sathiyaraman M                     [C#]      SathiyaramanM.BlazorWpf.Templates                <1k 

C# Class                                       class                                 Jacob Viau                         [C#]      Vio.Dotnet.Templates                              2k 

...

To use the template, run the following command to install the package:

   dotnet new --install <PACKAGE_ID>

Example:

   dotnet new --install Microsoft.Maui.Templates

应该从NuGet.org搜索到了名字里带Class的模板。比如我现在是创建Class失败,那么我们找到Class的模板和它对应的包名为Vio.Dotnet.Templates。根据提示安装包:

dotnet new --install Vio.Dotnet.Templates

那么我们安装这个包之后就能成功创建Class文件了。