Async Controller Returning “System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]”

孤街浪徒 提交于 2019-12-12 06:13:45

问题


I'm trying to work out why Umbraco 7.2.4 just doesn't seem to handle asynchronous tasks in my ASP.NET MVC controller. I feel like I've read almost every possible stack overflow and umbraco q&a, and tried many possible methods to try narrow down the problem. This is both for Umbraco 7 & MVC 4 and & MVC 5. It works just fine in an MVC project without Umbraco.

HomeController.cs:

using System.Threading.Tasks;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace Umbraco.Async.Website.Controllers
{
    public class HomeController : RenderMvcController
    {
        public new async Task<ActionResult> Index(RenderModel model)
        {
            var menuModel = new HomeViewModel(model);
            await Task.Delay(1000);
            return View("Home", menuModel);
        }
    }

    public class HomeViewModel : RenderModel
    {
        public string Test = "Pizza is awesome!!!!";

        public HomeViewModel(RenderModel model)
            : base(model.Content, model.CurrentCulture)
        {

        }
    }
}

Home.cshtml:

@*@inherits Umbraco.Web.Mvc.UmbracoTemplatePage*@
@inherits UmbracoViewPage<Umbraco.Async.Website.Controllers.HomeViewModel>
@{
    Layout = null;
}

<h1>@Model.Test</h1>

In the end the browser shows no rendered view, and just the text string:

System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]


回答1:


Do you have this key in your web.config file? If not, please add it and then try.

<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>



回答2:


I've raised an issue with Umbraco for this issue as I have ben struggling with this for quite a while. I have written an article on a solution here but in a nutshell you can use this instead:

public async Task<ActionResult> Home(RenderModel model)
{
    var menuModel = new HomeViewModel(model);
    await Task.Delay(1000);
    return View("Home", menuModel);
}

The difference being that the Home action takes precedence over the failing Index action as it is routed from the template name not the document type alias.



来源:https://stackoverflow.com/questions/30166566/async-controller-returning-system-threading-tasks-task1system-web-mvc-actionr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!