DHo*_*itz 4 webserver dependency-injection razor web blazor
我正在尝试创建一个错误页面服务,当调用该服务时,它将确定发生了哪种类型的异常并正确处理代码。如果异常是“重定向”类型,它应该将用户发送到登录页面(出于测试目的,我仅使用主页“/”页面)。
我正在尝试使用 NavigationMangager.NavigateTo("/") 但每当我到达该函数调用时就会遇到空点异常。我一直在寻找解决方案,但我看到的所有内容都是在 .razor 文件中使用 NavigationManager,并且我正在尝试在 .cs 服务中完成此操作。目前这是不可能的还是我只是做错了什么?我已经在下面的文件“ErrorProcessingService.cs”中包含了所有相关代码,任何帮助将不胜感激。
using ToDoList.Pages;
using Microsoft.AspNetCore.Components;
namespace ToDoList.ExceptionHandler
{
public class ErrorProcessingService
{
[Inject]
protected NavigationManager NavigationManager {get; set;}
public void processError(ErrorTypes.errorType et)
{
new ErrorTypes();
switch(et)
{
case ErrorTypes.errorType.ignore:
//To Be Implemented
break;
case ErrorTypes.errorType.popup:
//To Be Implemented
break;
case ErrorTypes.errorType.redirect:
NavigationManager.NavigateTo("/");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在服务中,您不能使用[Inject]属性,但可以在构造函数中注入依赖项:
using ToDoList.Pages;
using Microsoft.AspNetCore.Components;
namespace ToDoList.ExceptionHandler
{
public class ErrorProcessingService
{
public ErrorProcessingService(NavigationManager navigationManager)
{
NavigationManager = navigationManager;
}
protected NavigationManager NavigationManager { get; }
public void processError(ErrorTypes.errorType et)
{
new ErrorTypes();
switch(et)
{
case ErrorTypes.errorType.ignore:
//To Be Implemented
break;
case ErrorTypes.errorType.popup:
//To Be Implemented
break;
case ErrorTypes.errorType.redirect:
NavigationManager.NavigateTo("/");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5309 次 |
| 最近记录: |