简体   繁体   English

Razor 异步获取的页面 model 是 null

[英]Razor Pages on async Get model is null

I'm having interesting problem with Razor pages on Asp.Net Core 2.2.我在 Asp.Net Core 2.2 上的 Razor 页面遇到了有趣的问题。 I have a page (not partial, normal page) with async get:我有一个带有异步获取的页面(不是部分的正常页面):

    public async Task<IActionResult> OnGetAsync()
    {
        Accounts = new List<DbAccount>();

        AccountRequest accountRequest = new AccountRequest(User.FindFirst("BearerToken").Value);
        await ClientRequest.ExecuteRequest(accountRequest);

        if (accountRequest.StatusCode != HttpStatusCode.OK)
        {
            ModelState.AddModelError("ERROR", "Login error");
            return Page();
        }
        else
        {
            var data = JsonConvert.DeserializeObject<List<DbAccount>>(accountRequest.ResponseContent);
            Accounts.AddRange(data);
            return Page();
        }
    }

Get is executed normally and there is data to add in Accounts.AddRange() but later in page, when I try to iterate over that collection, Model is null. Get 正常执行,并且在 Accounts.AddRange() 中有要添加的数据,但在页面后面,当我尝试迭代该集合时,Model 是 null。

 @foreach(var account in Model.Accounts)
 {
     <tr>
     ...
     </tr>
 }

And of course, I get NullReferenceException on Model.Accounts.当然,我在 Model.Accounts 上得到 NullReferenceException。 Where is the problem?问题出在哪里? In same project, in normal Get without async, Model is not null.在同一个项目中,在没有异步的正常获取中,Model 不是 null。 Propery Accounts is set as BindProperty .属性Accounts设置为BindProperty

What I'm doing wrong?我做错了什么?

Did you try to add a new razor page item to test?您是否尝试添加新的 razor 页面项目进行测试? If _ everything else is "standard"_, it should get the value normally.如果_其他一切都是“标准”_,它应该正常获取值。

There is no actual difference between OnGet and OnGetAsync . OnGetOnGetAsync之间没有实际区别。 OnGetAsync is just a naming convention for methods that contain asynchronous code that should be executed when a GET request is made. OnGetAsync只是包含异步代码的方法的命名约定,该代码应在发出 GET 请求时执行。 You can omit the Async suffix but still make the method asynchronous:您可以省略Async后缀,但仍使方法异步:

public async Task OnGet()
{
    ...
    await ....
    ...
}

Asynchronous methods are ones that free up their threads while they are executing so that it can be used for something else until the result of the execution is available.异步方法是在它们执行时释放它们的线程以便它可以用于其他事情直到执行结果可用的方法。 You can read more about how asynchronous methods work here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/#BKMK_WhatHappensUnderstandinganAsyncMethod您可以在此处阅读有关异步方法如何工作的更多信息: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/#BKMK_WhatHappensUnderstandinganAsyncMethod

You can't have an OnGet and an OnGetAsync handler in the same Razor Page.您不能在同一个 Razor 页面中拥有OnGetOnGetAsync处理程序。 The framework sees them as being the same.该框架认为它们是相同的。

Problem solved by changing from async Task<IActionResult to async Task (and removing returns).通过从async Task<IActionResultasync Task (并删除返回)解决了问题。 Anyone can explain what is the difference and why it helped?任何人都可以解释有什么区别以及为什么有帮助?

I had the same problem with public async void OnGetAsync() As soon as I added an await in het function I got a nullreference exception on the model.我对 public async void OnGetAsync() 有同样的问题只要我在 het function 中添加了一个等待,我就在 model 上遇到了一个空引用异常。

Replacement with public async Task OnGetAsync() did solve my exception.用公共异步任务 OnGetAsync() 替换确实解决了我的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM