基本操作

以新建console项目为例:

dotnet new console -o test

通常,如果不知道某个命令怎么写可直接通过dotnet --help获得帮助。如果不知道某个命令参数对应的参数,直接运行不完整参数即可获得帮助。例如运行dotnet new,即可获得如下详细的说明:

Templates                                         Short Name       Language          Tags
--------------------------------------------------------------------------------------------------------
Console Application                               console          [C#], F#, VB      Common/Console
Class library                                     classlib         [C#], F#, VB      Common/Library
Unit Test Project                                 mstest           [C#], F#, VB      Test/MSTest
xUnit Test Project                                xunit            [C#], F#, VB      Test/xUnit
ASP.NET Core Empty                                web              [C#], F#          Web/Empty
ASP.NET Core Web App (Model-View-Controller)      mvc              [C#], F#          Web/MVC
ASP.NET Core Web App                              razor            [C#]              Web/MVC/Razor Pages
ASP.NET Core with Angular                         angular          [C#]              Web/MVC/SPA
ASP.NET Core with React.js                        react            [C#]              Web/MVC/SPA
ASP.NET Core with React.js and Redux              reactredux       [C#]              Web/MVC/SPA
ASP.NET Core Web API                              webapi           [C#], F#          Web/WebAPI
global.json file                                  globaljson                         Config
NuGet Config                                      nugetconfig                        Config
Web Config                                        webconfig                          Config
Solution File                                     sln                                Solution
Razor Page                                        page                               Web/ASP.NET
MVC ViewImports                                   viewimports                        Web/ASP.NET
MVC ViewStart                                     viewstart                          Web/ASP.NET

Examples:
    dotnet new mvc --auth Individual
    dotnet new xunit
    dotnet new --help

常见命令

添加nuget项目

dotnet add package [项目名]

添加项目依赖

dotnet add reference [项目文件(*.csproj)路径]

疑难解答

  • 编译时报错:MSBUILD : error MSB1025: An internal failure occurred while running MSBuild.
MSBUILD : error MSB1025: An internal failure occurred while running MSBuild.
System.InvalidOperationException: The terminfo database is invalid.
   at System.TermInfo.Database..ctor(String term, Byte[] data)
   at System.TermInfo.Database.ReadDatabase(String term, String directoryPath)
   at System.TermInfo.Database.ReadDatabase(String term)
   at System.TermInfo.Database.ReadActiveDatabase()
   at System.ConsolePal.TerminalFormatStrings.<>c.<.cctor>b__27_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.ConsolePal.EnsureInitializedCore()
   at System.ConsolePal.ControlCHandlerRegistrar.Register()
   at System.Console.add_CancelKeyPress(ConsoleCancelEventHandler value)
   at Microsoft.Build.CommandLine.MSBuildApp.Execute(String[] commandLine)

Unhandled Exception: System.InvalidOperationException: The terminfo database is invalid.
   at System.TermInfo.Database..ctor(String term, Byte[] data)
   at System.TermInfo.Database.ReadDatabase(String term, String directoryPath)
   at System.TermInfo.Database.ReadDatabase(String term)
   at System.TermInfo.Database.ReadActiveDatabase()
   at System.ConsolePal.TerminalFormatStrings.<>c.<.cctor>b__27_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.ConsolePal.EnsureInitializedCore()
   at System.ConsolePal.ControlCHandlerRegistrar.Register()
   at System.Console.add_CancelKeyPress(ConsoleCancelEventHandler value)
   at Microsoft.Build.CommandLine.MSBuildApp.Execute(String[] commandLine)
   at Microsoft.Build.CommandLine.MSBuildApp.Main(String[] args)

暂不清楚原因,临时解决方案为修改环境变量TERMxterm

TERM=xterm

运行.NET Core程序时也可能遇到类似问题,同样可通过该方法解决。

  • 编译时报错:

error : The current .NET SDK does not support
targeting .NET Core 2.1.  Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.

这是由于被使用的dotnet SDK无法编译指定版本的代码(2.1),通常由SDK未更新引起,可通过手动降级*.csproj文件解决。需要注意的是,降级也会导致新版本的特性无法得到使用。

降级*.csproj文件只需要手动修改其TargetFramework
字段即可:

<TargetFramework>netcoreapp2.0</TargetFramework>

发表评论

电子邮件地址不会被公开。 必填项已用*标注