ASP.NET 核心 CRUD 工具:未找到命令但实际已安装

ASP.NET Core CRUD tool: command not found but actually installed

我正在学习ASP.net,所以我正在阅读https://docs.microsoft.com/fr-fr/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-3.0&tabs=visual-studio-code并且在脚手架电影模型部分,我必须安装和运行 CRUD 工具(ASP.net Core dotnet-aspnet-codegenerator)。 我遇到的问题是无法使用他们的工具,输出错误;但是我可以安装它。下面是详细信息。

所以我按照三步教程来使用这个 CRUD 代码生成器(link 上面给出的)。我引用:

  1. Open a command window in the project directory (The directory that contains the Program.cs, Startup.cs, and .csproj files).

✔ 成功了。

  1. Install the scaffolding tool: dotnet tool install --global dotnet-aspnet-codegenerator

✔ 它工作正常,现在如果我尝试重新安装它,它会输出它已经安装。

  1. For macOS and Linux: Run the following command: dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries

没用。 这里是输出错误:

Could not execute because the specified command or file was not found. Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a .NET Core program, but dotnet-dotnet-aspnet-codegenerator does not exist. * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

问题:如何调试才能使此命令正常工作?

有时最新版本可能不一致。尝试使用旧版本,看看是否有任何区别。从当前版本之前的最高版本开始,然后向下寻找真正有效的版本。如何安装旧版本:

dotnet tool install --global dotnet-aspnet-codegenerator --version {version}

您可以在 nuget 页面中找到版本历史:
https://www.nuget.org/packages/dotnet-aspnet-codegenerator/

哦,哦... "Since you just installed the .NET Core SDK, you will need to logout or restart your session before running the tool you installed." - 我没看过,这是 CLI 在安装代码生成器时输出的。

更新: 3.0.0 和代码生成器的 2.1.10 :(...

export PATH=$HOME/.dotnet/tools:$PATH 是必需的,如 Core MVC (https://docs.microsoft.com/fr-fr/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-3.0&tabs=visual-studio-code) 所示。 Core Razor 忘记了这个指示。 . .

在 Windows 10

在我的例子中,安装程序向 Path 环境变量添加了错误的路径。
添加的路径指向程序下不存在的文件夹。它需要指向 dotnet-aspnet-codegenerator.exe.
对我来说,正确的路径在我的用户文件夹中:~\.dotnet\tools

您可以检查运行添加的路径是否正确:echo $env:Path
如果路径丢失或不正确,您只需将正确的路径添加到 Path 系统环境变量。

您可以通过使用 PowerShell 设置局部变量来对此进行测试:$env:Path += ";C:\Users\<YOUR_NAME_HERE>\.dotnet\tools"
但是我没试过这个


全局修复

  1. 开始在 windows 搜索中输入 Environment,您应该会看到 Edit system environment variablesControl panel 选项。
  2. 点击右下角的Environment Variables...按钮。
  3. System variables 下找到 select Path 变量,然后单击 Edit
  4. 检查 dotnet-aspnet-codegenerator.exe 的路径是否存在,如果不存在,请单击 New 并添加它。
  5. 重新启动计算机。