Blazor Server托管,null参考异常
我正在我们的一个项目上测试Blazor Server(.NET 6.05),并且我有一个以下代码:
@page "/"
@using Microsoft.EntityFrameworkCore
@using Microsoft.JSInterop
@inject IDbContextFactory<MyDbContext> DbContextFactory;
@code {
[Inject]
private IHttpContextAccessor HttpContextAccessor { get; set; } = default!;
MyDbContextContext { get; set; }
public Dish? RandomDish { get; set; }
public Region? Region { get; set; }
async Task getRandomDish()
{
var dish = await Context.Dishes.OrderBy(m => Guid.NewGuid()).FirstOrDefaultAsync();
RandomDish = dish;
}
async Task SaveDishName()
{
await Context.SaveChangesAsync();
}
protected override async Task OnInitializedAsync()
{
var a = HttpContextAccessor.HttpContext.User.Identity;
Context = await DbContextFactory.CreateDbContextAsync();
Region = await Context.Regions.SingleOrDefaultAsync(m => m.Name == "Italy");
await getRandomDish();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return;
}
}
<PageTitle>Index</PageTitle>
@if (RandomDish != null)
{
<h1>@RandomDish?.Name</h1>
<h2>@RandomDish?.DishId</h2>
<p>@Region?.Borders?.AsText()</p>
}<-- it errors here
<button @onclick="@(async () => await getRandomDish())">GET NEW DISH</button>
<div>
Change dish name
<input @bind="RandomDish.Name" />
<button @onclick="@(async () => await SaveDishName())">Save</button>
</div>
运行代码时,它会以异常为例。
system.nullReferenceException:'对象引用未设置为 对象的实例。'
****。cms.web.pages.index.randomdish.get返回null。
我还尝试了“不是零”和参考等式(Randomdish,null),但始终存在相同的问题。
我应该如何进行零检查,如果getters将nulls投掷:/
问题如果我用下线初始化菜肴,则会消失,但这不是解决问题的解决方案。
public Dish? RandomDish { get; set; } = new Dish();
I'm testing blazor server (.net 6.05) on one of our projects and I have a following code:
@page "/"
@using Microsoft.EntityFrameworkCore
@using Microsoft.JSInterop
@inject IDbContextFactory<MyDbContext> DbContextFactory;
@code {
[Inject]
private IHttpContextAccessor HttpContextAccessor { get; set; } = default!;
MyDbContextContext { get; set; }
public Dish? RandomDish { get; set; }
public Region? Region { get; set; }
async Task getRandomDish()
{
var dish = await Context.Dishes.OrderBy(m => Guid.NewGuid()).FirstOrDefaultAsync();
RandomDish = dish;
}
async Task SaveDishName()
{
await Context.SaveChangesAsync();
}
protected override async Task OnInitializedAsync()
{
var a = HttpContextAccessor.HttpContext.User.Identity;
Context = await DbContextFactory.CreateDbContextAsync();
Region = await Context.Regions.SingleOrDefaultAsync(m => m.Name == "Italy");
await getRandomDish();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return;
}
}
<PageTitle>Index</PageTitle>
@if (RandomDish != null)
{
<h1>@RandomDish?.Name</h1>
<h2>@RandomDish?.DishId</h2>
<p>@Region?.Borders?.AsText()</p>
}<-- it errors here
<button @onclick="@(async () => await getRandomDish())">GET NEW DISH</button>
<div>
Change dish name
<input @bind="RandomDish.Name" />
<button @onclick="@(async () => await SaveDishName())">Save</button>
</div>
When I run the code, it throws following exception
System.NullReferenceException: 'Object reference not set to an
instance of an object.'****.Cms.Web.Pages.Index.RandomDish.get returned null.
I also tried "is not null" and referenceEquals(RandomDish, null), but always the same issue.
How am I supposed to do a null check, if getters will throw nulls :/
Issue disappears if I initialize dish with line under, but that's not a solution to a problem I guess.
public Dish? RandomDish { get; set; } = new Dish();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在
@bind
中获得零。用
@if(RandomDish!= null)
或类似的东西包围保存块。You cannot have a null in
@bind
.Surround the Save block with
@if (RandomDish != null)
or something similar.您确定正确解释了标记中的表达方式吗?尝试
@(RandomDish?.name)
和@(Randomdish?.dishId)
no no no no no no no no no no。我怀疑@randomdish?.name
可能被解释为@randomdish
然后是字面的“?name”
。Are you sure your expressions in the markup are being interpreted rightly? Try
@(RandomDish?.Name)
and@(RandomDish?.DishId)
just to be sure. I suspect that@RandomDish?.Name
might be interpreted as@RandomDish
then a literal"?.Name"
.