在 Razor Pages 中使用 @functions 块的错误和警告

Jon*_*ood 4 c# async-await asp.net-core razor-pages

由于@helperASP.NET Core Razor Pages 不再支持该指令,因此我一直使用该@functions指令。

@functions
{
    void RenderTask(Models.Task task)
    {
        <tr>
            <td class="@Model.CssClass">
                <p class="compact">
                    <span class="font-weight-bold">@task.Title</span>
                    @if (!string.IsNullOrWhiteSpace(task.Description))
                    {
                        <br />@task.Description
                    }
                </p>
            </td>
            <td class="@Model.CssClass">
                <img src="~/images/Edit.png" class="edit-area button-img" data-id="@task.Id" title="Edit" />
                <img src="~/images/Delete.png" class="delete-area button-img" data-id="@task.Id" title="Delete" />
            </td>
        </tr>
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但我收到错误:

错误 MVC1006:该方法包含 TagHelper,因此必须是异步的并返回任务。例如,使用 ~/ 通常会产生 TagHelper,并需要异步任务返回父方法。

所以我将此函数更改为async,并在await调用它的任何地方使用了关键字。

@functions
{
    async System.Threading.Tasks.Task RenderTask(Models.Task task)
    {
        <tr>
            <td class="@Model.CssClass">
                <p class="compact">
                    <span class="font-weight-bold">@task.Title</span>
                    @if (!string.IsNullOrWhiteSpace(task.Description))
                    {
                        <br />@task.Description
                    }
                </p>
            </td>
            <td class="@Model.CssClass">
                <img src="~/images/Edit.png" class="edit-area button-img" data-id="@task.Id" title="Edit" />
                <img src="~/images/Delete.png" class="delete-area button-img" data-id="@task.Id" title="Delete" />
            </td>
        </tr>
    }
}
Run Code Online (Sandbox Code Playgroud)

这实际上有效,但我收到警告:

...\Razor\Pages\Tasks\Index.cshtml.g.cs(286,200,286,202):警告 CS1998:此异步方法缺少“await”运算符,将同步运行。考虑使用“await”运算符等待非阻塞 API 调用,或使用“await Task.Run(...)”在后台线程上执行 CPU 密集型工作。
...\Razor\Pages\Tasks\Index.cshtml.g.cs(312,200,312,202):警告 CS1998:此异步方法缺少“await”运算符,将同步运行。考虑使用“await”运算符等待非阻塞 API 调用,或使用“await Task.Run(...)”在后台线程上执行 CPU 密集型工作。

Index.cshtml.g.cs似乎是某种中间文件。但我不知道后面的数字是什么,双击这些警告不会将我带到有问题的行。

此时,我不确定问题出在哪里。我在谷歌上进行了广泛的搜索,但没有找到一个很好的例子来说明我应该做什么。任何建议表示赞赏。

更新:

这是Index.cshtml.g.cs的一部分:

#nullable restore
#line 86 "D:\Users\Jonathan\source\repos\Bamtok\Bamtok\Pages\Tasks\Index.cshtml"
                                                                          Write(task.Id);

#line default
#line hidden
#nullable disable
        __tagHelperStringValueBuffer = EndWriteTagHelperAttribute();
        __tagHelperExecutionContext.AddHtmlAttribute("data-id", Html.Raw(__tagHelperStringValueBuffer), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
        __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_3);
        await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); // *** ERROR HERE! ***
        if (!__tagHelperExecutionContext.Output.IsContentModified)
        {
            await __tagHelperExecutionContext.SetOutputContentAsync();
        }
        Write(__tagHelperExecutionContext.Output);
        __tagHelperExecutionContext = __tagHelperScopeManager.End();
        WriteLiteral("\r\n                ");
        __tagHelperExecutionContext = __tagHelperScopeManager.Begin("img", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "5fc6845fce9caf31066e5edd3fc6a51f323364e715810", async() => {
        }
        );
Run Code Online (Sandbox Code Playgroud)

Jon*_*ood 7

微软刚刚确认这些警告是一个错误。


小智 5

@functions 
{
    public async Task RenderItem(string date,string time,string id)
    {
        <td>@date</td>
        <td>@time</td>
        <td>
            <form method="post" class="form-inline" asp-page-handler="view" asp-route-id="@id">
                <button type="submit" class="btn btn-link">@id</button>
            </form>
        </td>
    }
}

Usage

 <tr>@{await RenderItem(@date.ToShortDateString(), time, id);}</tr>

Run Code Online (Sandbox Code Playgroud)