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

Server Blazor全局错误处理:lambda表达式转委托类型错误排查

Blazor全局捕获未处理错误时的委托转换异常排查

我尝试参考Stack Overflow上的问题《Is there a way to globally catch all unhandled errors in a Blazor single page application?》中的方案,但因声望不足无法在评论区跟进提问,想确认这些方案是否仍适用。

我的实现步骤

在Razor文件中使用最高赞答案的代码:

@{ OnError(@ex); }

并在代码后置文件(Shared\MainLayout.razor.cs)中编写对应方法:

public bool OnError(Exception ex)
{
    // 此处编写上报异常给开发团队/日志记录的代码
    return true;
}

我也曾尝试将方法改为void返回值,但遇到了相同错误。

遇到的错误

出现如下编译错误:

无法将lambda表达式转换为预期的委托类型,因为代码块中的某些返回类型无法隐式转换为委托的返回类型

错误提示指向文件Shared_MainLayout_razor.g.cs的第255行,但该文件实际不存在;而真实存在的Shared\MainLayout.razor.cs仅有约150行代码。

已排查内容

我查看过另一篇相关问题《Cannot convert lambda expression to intended delegate type because some of return types in block aren't implicitly convertible to delegate ret-type》,但我的情况与之不同:

  • 不存在可空类型相关问题
  • 调整Exception参数时,系统明确提示需要Exception类型,因此参数部分是正确的

请问有什么排查思路?感谢帮助。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 06:18:28