吐槽一下最近开发blazor SPA应用遇到的坑

跳动de指尖 跳动de指尖 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我用的是 Ant Design of Blazor

首先,我觉得blazor最大的问题就是断点偶尔失效的问题,这点很烦,不能debug,只能从堆栈信息里面找到底哪里出错了。

.net standar API好少,NuGet上基于standar的库能用的也不多。


还有导航,在当前页导航到当前页是不会触发OnInitializedAsync,相当于js的pushState() 但是没有任何配套功能

[Inject]
private NavigationManager Navigation { get; set; }

private asycn Task QueryAsycn()
{

/* 如果我当前页是 /Demo
* 那么我想筛选数据,导航到 /Demo?name=123,地址会变,但是不会OnInitializedAsync
* 不知道是我的想法没跟上还是本该这样子
*/
Navigation.NavigateTo("/Demo?name=123");
//如果导航到其他页面倒是符合预期,像这样
Navigation.NavigateTo("/Welcome?name=123");

//所以目前我处理的方式是 navigateTo 后,继续查询数据更新UI
Source = await LoadDataAsync();
}


RenderFragment 用代码的方式构建好麻烦,我开发的方式是
Welcome.razor
Welcome.razor.cs

//file Welcome.razor.cs
public partial class Webcome{}

就想着以后好维护,层次看的清晰,更纯粹干净,但是不得已还是在razor上添加了代码

//在razor文件上构建RenderFragment很简单 ,var rf = @<tag></tag>
private string NewTag { get; set; }

private RenderFragment DropdownRender(RenderFragment originNode)
{
RenderFragment customDropdownRender =
@<Template>
<div>
@originNode
<Divider Style="margin: 4px 0"></Divider>
<div style="display: flex; flex-wrap: nowrap; padding: 8px">
<Input Style="flex: auto" @bind-Value="NewTag"/>
<a style="flex: none; padding: 8px; display: block; cursor: pointer" @onclick="AddItem">
<Icon Type="plus" Theme="outline"></Icon>
添加标签
</a>
</div>
</div>
</Template>;

return customDropdownRender;
}

private void AddItem(MouseEventArgs args)
{
if (!string.IsNullOrWhiteSpace(NewTag))
{
_tags.AddItem(NewTag);
NewTag = string.Empty;
}
}

//但是在cs文件里面构建很麻烦,RenderFragment是一颗结构树,就简单一个图标都没那么便捷
var icon = builder =>
{
builder.OpenComponent<Icon>(0);
builder.AddAttribute(0,"Type","exclamation-circle");
builder.AddAttribute(1,"Theme","outline");
builder.CloseComponent();
},


就我上面的例子,就为了在下拉框新增一个值,把我坑惨了,花了我半天时间,就为了实现这么一个功能而已

然后就报错

因为什么? 因为 NewTag最开始我定义的是字段,不是属性
private string _newTag = string.Empty;
花半天时间浪费在这儿了。

还有一个路由问题,因为我要管理一些文件,所以路由参数值里面必然会有(. dot 点 )这个符号,然后404了。
嗯,想了一下,肯定是当作静态文件访问给处理了,没问题。
然后我去MSDN找文档看有什么解决办法
刚好也有。
引用
Routing with URLs that contain dots
For hosted Blazor WebAssembly and Blazor Server apps, the server-side default route template assumes that if the last segment of a request URL contains a dot (.) that a file is requested. For example, the URL https://localhost.com:5001/example/some.thing is interpreted by the router as a request for a file named some.thing. Without additional configuration, an app returns a 404 - Not Found response if some.thing was meant to route to a component with an @page directive and some.thing is a route parameter value. To use a route with one or more parameters that contain a dot, the app must configure the route with a custom template.

Consider the following Example component that can receive a route parameter from the last segment of the URL.

@page "/example/{param?}"

<p>
Param: @Param
</p>

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

照做,然后喜闻乐见了,依然404
目前还没解决,哈哈 。等会儿还是解决不了,我就采用QueryString的方式吧。





哈哈哈哈,写了几天了,想吐槽 吐槽 吐槽 吐槽
...全文

相关推荐

Blazor Hydra-在单个站点上托管多个Blazor SPA

如何在同一屋檐下合并Razor,Blazor WASM和服务器SPA

weixin_38702339

使用 Dapper 创建 Blazor Server SPA

使用 Dapper 创建 Blazor Server SPA的实例

谢.锋

Blazor开发实战指南

本书是David Pine所著《Blazor开发实战指南》的早期发布版本,旨在向.NET和Web开发者介绍Blazor框架。Blazor允许开发者使用C#、HTML和CSS构建单页应用程序(SPA),与传统的JavaScript框架如Angular、React、VueJS和Svelte形成竞争。作者详细讲解了WebAssembly技术如何使C#能够在浏览器中运行,并强调了.NET生态系统的优势。本书适合有实际Web应用开发经验的开发者,特别是那些希望利用现有.NET技能进行Blazor开发的.NET开发者,以及对Blazor感兴趣的Web开发者。

Salton Z

MetroBlazor:DC Metro Blazor应用程序

MetroBlazor DC Metro Blazor应用

缪建明

Blazor-CRUD-With-CloudFirestore:在Google Cloud Firestore的帮助下使用Blazor作为数据库提供程序的单页应用程序(SPA)

Blazor-CRUD-with-CloudFirestore 我们将使用Google Cloud Firstore作为数据库提供程序来创建Blazor应用程序。 我们将创建一个单页应用程序(SPA)并对其执行CRUD操作。 我们将使用表格接受用户输入。 该表单将具有客户端验证,并包含一个下拉列表,该列表将绑定到我们数据库中的集合。 我们将使用Visual Studio 2019。 演示版 阅读全文,网址为

向着程序媛生长的
评论 5
收藏 已收藏
分享
返回顶部
跳动de指尖
C# 111,129 成员 642,538 内容