当组件参数太大时,Blazor无法连接到服务器

发布于 2025-02-03 00:14:35 字数 878 浏览 1 评论 0原文

我正在运行一个带有一些大型组件的.NET 6 MVC应用程序。除一个外,它们都可以正常工作。

从视图的角度来看,我将有问题的组件嵌入了两个参数,学生和Isuserpartner。

<component 
    type="typeof(IndexRoster)" 
    render-mode="ServerPrerendered" 
    param-Students="@Model.Students" 
    param-IsUserPartner="@User.IsInRole("Partner")" 
/>

该组件最初似乎使得良好,但是在一两秒钟后,应用程序断开连接,并且页面显示了消息,“试图重新连接到服务器...”

我发现这是学生参数是罪魁祸首,因为当我删除它时,应用程序正常。此参数是DTOS的列表:

public class StudentIndexDTO {
    public List<StudentDisplayDTO> Students { get; set; }
    // ...
}

什么是最有趣的是,用param-students =“@model.students.getrange(0,10)限制了学生列表中的项目数量“,还解决了问题。因此,看来列表本身不是问题,而是其大小。不幸的是,我不能限制从学生列表中获取的物品的数量;该应用需要显示所有内容(400-1,000)。

我唯一能够提出的其他解决方案是将渲染模式设置为static,但同样,这不是一个足够的解决方案,因为它删除了其他必要的功能。

那么,如何将完整列表传递给我的组件,而不会导致应用程序断开连接?

I'm running a .NET 6 MVC app with a few Blazor components. All of them work fine, except one.

In a view, I embed the problematic component like so, passing in two parameters, Students and IsUserPartner.

<component 
    type="typeof(IndexRoster)" 
    render-mode="ServerPrerendered" 
    param-Students="@Model.Students" 
    param-IsUserPartner="@User.IsInRole("Partner")" 
/>

The component seems to render fine initially, but after a second or two the app disconnects and the page displays the message, "Attempting to reconnect to the server...".

I figured out that it's the Students parameter that's the culprit, because when I remove it, the app works fine. This parameter is a List of DTOs:

public class StudentIndexDTO {
    public List<StudentDisplayDTO> Students { get; set; }
    // ...
}

What's most interesting is that substantially limiting the amount of items in the Students List with param-Students="@Model.Students.GetRange(0, 10)", also fixes the issue. So, it seems that it's not a problem with the List per se, but rather with its size. Unfortunately, I can't limit the amount of items fetched from the Student List; the app needs to display all of them (400 - 1,000).

The only other solution I was able to come up with is setting the render mode to Static, but again, that's not an adequate solution since it removes other necessary functionalities.

So, how can I pass the full List to my component without causing the app to disconnect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-02-10 00:14:35

由于@mistermagoo的建议,我似乎找到了解决方法。在on InitializedAsync()中,我使用httpclient在 on InitializedAsync() 中提出了异步请求,然后在响应中填充学生列表。

I seem to have found a workaround thanks to the suggestion of @MisterMagoo. In the Blazor component, within OnInitializedAsync(), I made an async request to a backend Controller Action using HttpClient, which then populates the Students List with the response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文