超文本标记语言动作抛出“异步操作”;InvalidOperationException

本文关键字:异步操作 InvalidOperationException 超文本标记语言 | 更新日期: 2023-09-27 18:01:39

我真的被这个卡住了。

我的动作结果如下

public ActionResult HolidayReport()
{
    return View("~/Views/Employee/GetEmployeeHolidayReport.cshtml");
}

当前的视图内容为

<div class="panel panel-default panel-main">
    <div class="panel-body">
        @{ Html.RenderAction("Test"); }        
    </div>
</div>

为完成起见,这是Test动作

public ActionResult Test()
{
    return new EmptyResult();
}

甚至没有去到控制器或任何类型的它抛出了异常

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

在视图中的@{ Html.RenderAction("Test"); }部分。将它替换为@Html.Action()也会导致同样的问题。可能会发生什么?

超文本标记语言动作抛出“异步操作”;InvalidOperationException

Test动作大概是异步的(返回Task<T>)。ASP不支持异步子操作。Net MVC 4。请注意,ASP支持异步视图组件。网络核心。

如果你还没有准备好迁移到ASP。那么你最好的选择是使Test同步。

我有同样的问题,它最终是由我继承的控制器引起的。父控制器的初始化方法被重写并标记为async。