You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 6 MVC添加Razor视图报错:未找到名为'view'的代码生成器

ASP.NET Core 6 MVC 添加Razor视图时出现"No code generators found with the name 'view'"错误

我使用VS 2022开发ASP.NET Core 6 MVC电商网站,商品展示、购物车等功能正常运行,但尝试为任意对象创建Razor视图(创建、编辑、列表)时,抛出以下错误:

No code generators found with the name 'view'
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

项目文件引用的包如下:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>bbd1355e-1244-42ae-9d11-e1c7a7bd0a2b</UserSecretsId>
    <StartupObject></StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.16">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.16">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NETCore.App" Version="2.1.30" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
</Project>

我已尝试卸载重装所有包、检查可用代码生成器(显示已全部安装),怀疑问题与Microsoft.AspNetCore.Identity.EntityFrameworkCore相关但该包是项目必需,搜索多日未解决,寻求帮助。


解决方案

1. 移除冲突的低版本依赖包

项目中引用的Microsoft.NETCore.App 2.1.30是多余且冲突的——ASP.NET Core 6项目默认已包含对应运行时,低版本包会干扰代码生成器的依赖加载。

  • 打开项目文件(.csproj),删除<PackageReference Include="Microsoft.NETCore.App" Version="2.1.30" />这一行
  • 保存文件后,右键项目选择清理,然后重启Visual Studio

2. 重新安装代码生成工具

确保全局的代码生成器工具版本与项目版本一致:

  1. 打开包管理器控制台,执行以下命令卸载旧工具:
    dotnet tool uninstall -g dotnet-aspnet-codegenerator
    
  2. 安装与项目匹配的6.0.16版本工具:
    dotnet tool install -g dotnet-aspnet-codegenerator --version 6.0.16
    
  3. 验证生成器是否可用:
    dotnet aspnet-codegenerator list
    
    输出中应包含View相关生成器

3. 修复Visual Studio组件

确认VS的Web开发组件完整:

  • 打开Visual Studio安装器,选择修改
  • 检查.NET ASP.NET和Web开发组件是否勾选,若已勾选,点击修改选择修复
  • 等待修复完成后重启VS

4. 验证项目依赖一致性

确保所有ASP.NET Core相关包的版本均为6.0.16,避免版本不匹配导致的加载问题。


内容的提问来源于stack exchange,提问作者mer063

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:35:28