0

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]

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
legas
  • 408
  • 4
  • 10
  • 1
    See this question elsewhere on SO http://stackoverflow.com/questions/23006976/async-controller-action-with-umbraco-7-returns-string – ProNotion May 11 '15 at 13:10
  • Hi @ProNotion, thanks for that, I've tried both solutions in that question on multiple projects (MVC4, MVC5) before with no luck. I will add a comment to their question. Cheers! – legas May 11 '15 at 21:36
  • ....Also I've tried with a SurfaceController (as in http://stackoverflow.com/questions/23006976/async-controller-action-with-umbraco-7-returns-string) as well as my RenderMvcController. (extra info that might be useful) – legas May 11 '15 at 21:43
  • @legas: Are you running on .NET 4.5 or newer? – Stephen Cleary Oct 17 '15 at 14:24

2 Answers2

0

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>
Keval Gangani
  • 1,326
  • 2
  • 14
  • 28
0

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.

Digbyswift
  • 10,310
  • 4
  • 38
  • 66