Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blazor 如果http GetById返回404,则导航到NotFound页面_Blazor_Blazor Client Side - Fatal编程技术网

Blazor 如果http GetById返回404,则导航到NotFound页面

Blazor 如果http GetById返回404,则导航到NotFound页面,blazor,blazor-client-side,Blazor,Blazor Client Side,假设我有以下路由模式 CustomerListComponent:包含客户列表 CustomerDetailComponent:保存特定客户的详细信息 有路线 @第页“客户” @第页“客户/{id:int}” 以及路由器设置,如下所示: <Router AppAssembly="typeof(Startup).Assembly"> <Found Context="routeData"> .... </Found>

假设我有以下路由模式

  • CustomerListComponent:包含客户列表
  • CustomerDetailComponent:保存特定客户的详细信息
有路线

  • @第页“客户”

  • @第页“客户/{id:int}”

以及路由器设置,如下所示:

<Router AppAssembly="typeof(Startup).Assembly">
    <Found Context="routeData">
        ....
    </Found>
    <NotFound>
        <p>Sorry, there's nothing at this address.</p>
    </NotFound>
</Router>
现在看来,URL中的任何有效整数都会打开CustomerDetail组件,当它试图显示客户详细信息时,它(显然)会因
null引用而崩溃

我错过了什么明显的东西吗


编辑:我尝试加载一个自定义404页面并在我的http服务中处理导航,但这会导致浏览器上的URL更改。我希望保留错误的URL并仍然导航到404页面。

您可以使用
导航管理器

@inject NavigationManager\u nagigationManager;
...
@代码{
受保护的重写异步任务OnInitializedAsync()
{
var customer=await CustomerService.GetById(customerId);
如果(客户==null)
{
_navigationManager.NavigateTo(“404页”);
}
}
}
如果希望保持在同一页面上,但显示错误页面的内容,只需在html代码中添加组件:

@if(\u未找到)
{
}
其他的
{
...
}
@代码{
未找到私人布尔;
受保护的重写异步任务OnInitializedAsync()
{
var customer=await CustomerService.GetById(customerId);
如果(客户==null)
{
_notFound=true;
StateHasChanged();
}
}
}

您可以使用
NavigationManager

@inject NavigationManager\u nagigationManager;
...
@代码{
受保护的重写异步任务OnInitializedAsync()
{
var customer=await CustomerService.GetById(customerId);
如果(客户==null)
{
_navigationManager.NavigateTo(“404页”);
}
}
}
如果希望保持在同一页面上,但显示错误页面的内容,只需在html代码中添加组件:

@if(\u未找到)
{
}
其他的
{
...
}
@代码{
未找到私人布尔;
受保护的重写异步任务OnInitializedAsync()
{
var customer=await CustomerService.GetById(customerId);
如果(客户==null)
{
_notFound=true;
StateHasChanged();
}
}
}

这是否保留了引发浏览器上404的错误url?我希望通过将url更改为“../my not found page/”来避免丢失信息。您可以在查询字符串中设置错误url的路径。我将向上投票,但这会导致奇怪的行为。例如,如果我的@page有一个int限制,如路径“customer/{id:int}如果传递的Id大于int32,则将通过此代码查找错误的Id,但将通过标准的not found部分。我宁愿让它在一个地方处理,而不是在两个不同的地方处理。@WASM或服务器上的IoannisStefanou?这是否保留了导致浏览器上404的错误url?我希望通过将url更改为“../my not found page/”来避免丢失信息。您可以在查询字符串中找到错误的url。我将向上投票,但这会导致奇怪的行为。例如,如果我的@page有一个int限制,比如路径“customer/{id:int},这将通过此代码查找错误的id,但如果传递的id大于int32,则将通过标准的not found部分。我宁愿在一个地方处理它,而不是在两个不同的地方处理。@WASM或服务器上的IoannisStefanou?
protected override async Task OnInitializedAsync()
{
   var customer = await CustomerService.GetById(customerId);
   if (customer == null) 
   {
      // redirect to 404 NOTFOUND         
   }
}