在 asp.net core 3.1 异步编程中使用带有 bootstrap 4 的徽章

问题描述

我在没有异步方法的asp.net core 3.1中使用了徽章这是我的服务代码

 public int GetNotSeenContact()
    {
        return _context.Contacts.Where(s => s.Seen == false).Count();
    }

这是作用域布局中的调用

 public int GetContactCont()
    {
        return _admin.GetNotSeenContact();
    }

这是我使用它的地方:

 int ConactBell = scope.GetContactCont();

这是我调用的 html 代码

 <span class="badge badge-pill badge-warning notification">@ConactBell</span>

这工作得很好,但是当我使用 Async 方法实现它时,它显示此异常而不是用户编号:

System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[system.int32,DIG.Core.Classes.PanelLayoutScope+&lt;GetContactCont&gt;d__3]

在这些是我在服务中使用异步方法代码

 public async Task<int> GetNotSeenContact()
    {
        return await _context.Contacts.Where(s => s.Seen == false).CountAsync();
    }

这是在我的范围类中:

 public async Task<int> GetContactCont()
    {
        return await _admin.GetNotSeenContact();
    }

这是我在 html 代码中使用它的代码

@inject PanelLayoutScope scope
@{
Task<int> ConactBell = scope.GetContactCont();}

我的 html 代码

<span class="badge badge-pill badge-warning notification">@ConactBell</span>

请给我最好的解决方案,并告诉我为什么当我使用 Async 方法时会发生这种情况。非常感谢!

解决方法

因为方法是异步的,可以尝试使用await.Change

@{
Task<int> ConactBell = scope.GetContactCont();}

@{
    var ConactBell =await scope.GetContactCont();}

结果:

enter image description here