Blazor中的路由不起作用,传递的参数始终为null。

huangapple go评论54阅读模式
英文:

Routing in Blazor does not work, parameter passed is always null

问题

以下是翻译的内容:

我有一个看起来像这样的简单的Razor组件

    @page "/einsatz"
    @page "/einsatz/{ScaledTable}"

    @code {
        [Parameter] 
        public string ScaledTable { get; set; } 

        private string Test { get; set; }

        protected override async Task OnInitializedAsync()
        {
            Test = ScaledTable;   // ScaledTable在这里为NULL
        }

        protected override async Task OnParametersSetAsync()
        {
            Table = ScaledTable;  // ScaledTable在这里为NULL
        }
    }

我尝试使用以下URL调用组件

    https://localhost:60711/einsatz 

=> `ScaledTable` 如预期地为null

    https://localhost:60711/einsatz/test 

=> 异常

> 未能加载资源:服务器以状态码404响应 () all.css:1   
> 未能加载资源:服务器以状态码404响应 () site.css:1   
> 未能加载资源:服务器以状态码404响应 () blazor.server.js:1   
> ...等等
>        
> 未能加载资源:服务器以状态码404响应 ()  
> telerik-blazor.js:1 

    https://localhost:60711/einsatz?scaledtable=test 

=> 这里`ScaledTable`也为NULL - 为什么!?

如果我尝试以下页面指令:

    @page "/einsatz"
    @page "/einsatz/{ScaledTable:string}"

我在应用程序启动时会收到异常

> System.NullReferenceException:对象引用未设置为对象的实例。
>
> Microsoft.AspNetCore.Components.Routing.Router.Refresh(Boolean isNavigationIntercepted)  
> Microsoft.AspNetCore.Components.Routing.Router.SetParametersAsync(ParameterView parameters)

我在这里做错了什么?我在这里有什么误解?

**编辑** 2023226/18:03

首先,对于

    @page "/einsatz/{ScaledTable:string}"

的错误是设计上的,因为您不必为参数指定字符串类型。

进一步研究异常,使用

    @page "/einsatz/{ScaledTable}"

我发现这些错误来自于布局中生成的错误路由。尝试加载

    localhost:60711/einsatz/_framework/blazor.server.js

失败,HTTP 404错误,因为在此URL中不应该有"einsatz",似乎有些地方出现了问题...
英文:

I have a simple razor component looking like this

@page "/einsatz"
@page "/einsatz/{ScaledTable}"
@code {
[Parameter] 
public string ScaledTable { get; set; } 
private string Test { get; set; }
protected override async Task OnInitializedAsync()
{
Test = ScaledTable;   // ScaledTable is NULL here
}
protected override async Task OnParametersSetAsync()
{
Table = ScaledTable;  // ScaledTable is NULL here
}
}

I try to call the component with the following URLs

https://localhost:60711/einsatz 

=> ScaledTable is null as expected

https://localhost:60711/einsatz/test 

=> Exception

> Failed to load resource: the server responded with a status of 404 () all.css:1
> Failed to load resource: the server responded with a status of 404 () site.css:1
> Failed to load resource: the server responded with a status of 404 () blazor.server.js:1
> ... and so on
>
> Failed to load resource: the server responded with a status of 404 ()
> telerik-blazor.js:1

https://localhost:60711/einsatz?scaledtable=test 

=> ScaledTable is NULL here also - why!?

If I try this page directives instead:

@page "/einsatz"
@page "/einsatz/{ScaledTable:string}"

I get an exception at startup of the app

> System.NullReferenceException: Object reference not set to an instance of an object.
>
> at Microsoft.AspNetCore.Components.Routing.Router.Refresh(Boolean isNavigationIntercepted)
> at Microsoft.AspNetCore.Components.Routing.Router.SetParametersAsync(ParameterView parameters)

What am I doing wrong here? What I am misunderstanding here?

Edited 26.02.2023/18:03

First of all, the error for

@page "/einsatz/{ScaledTable:string}"

is by design, since you have not to specify string type for parameter.

Looking further into the exception, using

@page "/einsatz/{ScaledTable}"

I found out, that these errors come from wrong routes, generated for the files in the layout. The try to load

localhost:60711/einsatz/_framework/blazor.server.js

fails with HTTP 404 of cause, because there should be no "einsatz" in this URL, something seems to be wrong here anywhere ...

答案1

得分: 0

最后我找到了我犯的错误。

我需要在 _layout.cshtml 文件中添加以下行:

<head>
    <base href="~/" />
    ....

不知道我是在哪里丢失了这个,它是任何新的 Blazor 项目的一部分。

英文:

Finally I found the error I made.

I have to add the following line to the _layout.cshtml file:

&lt;head&gt;
&lt;base href=&quot;~/&quot; /&gt;
....

Don't know where I have lost this, it is part of any new Blazor project.

huangapple
  • 本文由 发表于 2023年2月26日 19:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定