Blazor Server托管,null参考异常

发布于 2025-02-06 07:51:50 字数 2164 浏览 1 评论 0原文

我正在我们的一个项目上测试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();

enter image description here

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

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

发布评论

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

评论(2

街角卖回忆 2025-02-13 07:51:50

您不能在@bind中获得零。

<input @bind="RandomDish.Name" />

@if(RandomDish!= null)或类似的东西包围保存块。

You cannot have a null in @bind.

<input @bind="RandomDish.Name" />

Surround the Save block with @if (RandomDish != null) or something similar.

面如桃花 2025-02-13 07:51:50

您确定正确解释了标记中的表达方式吗?尝试@(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".

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