Blazor项目复制Razor页面至客户端时遇Visual Studio异常求助
Blazor Web App复制Razor页面到Client项目时Visual Studio报错排查
问题描述
我是编程新手,跟着教程开发Blazor Web App项目,尝试将Server项目中的Razor页面复制到Client项目时,出现错误提示:Visual Studio ran into an unexpected problem,and may affect some project system functions。操作时未运行应用,怀疑问题出在Visual Studio设置或项目系统方面。
相关Razor文件代码
@page "/EditVeri" @page "/EditVeri/{id:int}" @inject IVeriService VeriService @rendermode InteractiveWebAssembly <h3>Veri Oluştur</h3> <br /> <EditForm Model="veri" OnSubmit="HandleSubmit" FormName="editVeri"> <label>Kodu</label> <InputText @bind-Value="veri.Kodu">@veri.Kodu</InputText> <br /> <label>Adı</label> <InputText @bind-Value="veri.Adı">@veri.Adı </InputText> <button type="submit" class="btn btn-primary">Submit</button> </EditForm> <br /> <br /> <a class="VeriOnay">Veri Eklendi</a> @code { [Parameter] public int id { get; set; } [SupplyParameterFromForm] Veris veri { get; set; } = new Veris { Adı = string.Empty, Kodu = string.Empty }; <style> </style> async Task HandleSubmit() { await VeriService.AddVeri(veri); await Task.Delay(2000); } }
报错日志
===================== 11.09.2024 14:01:05 Recoverable System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.VisualStudio.ProjectSystem.ProjectSerialization.CachedProjectItemDefinition.GetMetadata(String name) at Microsoft.VisualStudio.ProjectSystem.Items.SourceItemsService.<<GetAllLinkedItemsAsync>b__94_0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.ProjectLockService.<ExecuteWithinLockAsync>d__131`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.VisualStudio.ProjectSystem.ProjectLockService.<ExecuteWithinLockAsync>d__131`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.CopyPaste.DiskBasedCopyPasteItemHandler.<GatherExistingLinkedItemsPathAsync>d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.CopyPaste.DiskBasedCopyPasteItemHandler.<ValidateItemListAsync>d__26.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<>c__DisplayClass58_0.<<DoPasteCoreAsync>b__1>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.ProjectLockService.<ExecuteWithinLockAsync>d__130.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.VisualStudio.ProjectSystem.ProjectLockService.<ExecuteWithinLockAsync>d__130.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<DoPasteCoreAsync>d__58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<DoPasteAsync>d__57.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<>c__DisplayClass34_0.<<DropArea>b__1>d.MoveNext() ===================
排查解决步骤
清理项目缓存并重启VS:
- 关闭Visual Studio,删除项目目录下的
.vs、bin、obj文件夹 - 重新打开项目,尝试再次复制粘贴操作
- 关闭Visual Studio,删除项目目录下的
替换复制粘贴方式:
不要直接复制Server项目中的文件,而是在Client项目右键新建Razor组件,手动将原文件代码复制进去,避免VS项目系统识别异常检查服务依赖:
确认IVeriService在Client项目的Program.cs中已正确注册,且服务实现是WebAssembly可兼容的(不能直接使用Server端的EF Core等仅服务端可用的实现)修复Visual Studio:
打开VS安装程序,选择“修改”,点击“修复”选项,修复可能损坏的组件检查项目文件:
右键Client项目选择“编辑项目文件”,查看是否存在无效的项目项引用,删除异常条目后保存
内容的提问来源于stack exchange,提问作者LeventAydın
相关产品推荐
相关产品推荐

