在Razor页面中使用@functions块的错误和警告 [英] Errors and warnings using @functions block in Razor Pages
问题描述
由于ASP.NET Core Razor Pages中不再支持@helper
指令,所以我一直在使用@functions
指令.
Because the @helper
directive is no longer supported in ASP.NET Core Razor Pages, I've been using the @functions
directive instead.
@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>
}
}
这似乎可行,但出现错误:
This seems to work but I get an error:
错误MVC1006:该方法包含TagHelper,因此必须异步并返回Task.例如,〜/的使用通常会导致TagHelper,并且需要异步Task返回父方法.
Error MVC1006: The method contains a TagHelper and therefore must be async and return a Task. For instance, usage of ~/ typically results in a TagHelper and requires an async Task returning parent method.
因此我将此函数更改为async
,并且在任何被调用的地方都使用了await
关键字.
So I changed this function to be async
, and I used the await
keyword any place it is called.
@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>
}
}
这实际上有效,但是我得到警告:
This actually works but I get warnings:
... \ 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绑定的工作.
...\Razor\Pages\Tasks\Index.cshtml.g.cs(286,200,286,202): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
...\Razor\Pages\Tasks\Index.cshtml.g.cs(312,200,312,202): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
Index.cshtml.g.cs 似乎是某种中间文件.但是我不知道紧随其后的数字是什么,双击这些警告并不会把我带到令人讨厌的行.
Index.cshtml.g.cs appears to be some sort of intermediary file. But I don't know what the numbers are that follow it, and double clicking these warnings does not take me to the offending line.
在这一点上,我不确定是什么问题.我已经在Google上进行了广泛的搜索,但是没有找到一个很好的例子来说明我应该做的事情.任何建议表示赞赏.
At this point, I'm not sure what the problem is. I've Googled extensively but haven't found a good example of this that shows what I'm supposed to be doing. Any suggestions appreciated.
更新:
这是 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() => {
}
);
推荐答案
Microsoft 刚刚确认警告是一个错误.
Microsoft has just confirmed the warnings are a bug.
这篇关于在Razor页面中使用@functions块的错误和警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!