如何处理 blazor 服务器 razor 组件中未处理的异常,在页面模型中,我正在使用 try catch 块进行处理,但是一些错误来自 razor 文件并且电路中断,应用程序停止。请建议我处理这个问题的解决方案。
答案 0 :(得分:0)
我阅读了很多关于这个问题的文章,我发现现在无法为 blazor 进行全局错误处理,您可以像这样处理与 http 请求相关的错误:
if (!response.IsSuccessStatusCode)
{
var statusCode = response.StatusCode;
switch (statusCode)
{
case HttpStatusCode.NotFound: _navigation.NavigateTo("/Notfound", true);
break;
case HttpStatusCode.Unauthorized: _navigation.NavigateTo("/Error");
break;
default: _navigation.NavigateTo("/Error");
break;
}
throw new ApplicationException($"Reason: {response.ReasonPhrase}");
}
你还需要小心在 blazor 组件中使用 try catch 和一些探测器 [inject] 因为你的 blazor 电路可能会失败!。 您可以在 Feature Gates 中阅读更多相关信息,如果我错了,请更正。