Html.Action抛出“异步操作”InvalidOperationException

时间:2016-08-10 14:36:48

标签: c# asp.net-mvc-4 razor

我真的坚持这个。

我的行动结果如下

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()会导致同样的问题。可能会发生什么?

1 个答案:

答案 0 :(得分:0)

Test操作可能是异步的(返回Task<T>)。 Asynchronous child actions are not supported on ASP.NET MVC 4。请注意,ASP.NET Core上支持 异步视图组件。

如果您尚未准备好迁移到ASP.NET Core,那么最好的选择是让Test同步。