Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kieron McIntyre 110 posts 348 karma points
    Jun 23, 2015 @ 11:23
    Kieron McIntyre
    1

    Implementing an async custom controller in Umbraco 7

    I'm using Umbraco 7.2.5 and I have a very simple custom controller with a single Index() action but as soon as I attempt to make it async, it only ever renders the following:

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

    and not the intended view.

        public virtual async Task<ActionResult> Index(RenderModel model, ProgressModel progressModel)
        {
            await Task.Run(() =>
            {
                // Do work here
            });
    
            ...
    
        return CurrentTemplate(viewModel);
    }
    

    To clarify, I'm using the hybrid framework approach where each controller inherits from as base conttroller:

    public abstract partial class BaseSurfaceController
        : SurfaceController, IRenderMvcController
    {
        ...
    }
    

    I've done a lot of reading round and found references to there being a difference between .Net 4 and .Net 4.5 but I've checked and the website is definitely using .Net 4.5

    I have also seen this link and implemented the suggested fix but with no luck:

    https://our.umbraco.org/forum/ourumb-dev-forum/bugs/49922-Umbraco-v7-async-controller-action

    I should add that I have implemented an async SurfaceController in the same project and this works fine, so I'm assuming that it's an issue with IRenderMvcController possibly?

    Any help would be appreciated.

  • Tim 1193 posts 2655 karma points MVP c-trib
    Jun 23, 2015 @ 12:36
    Tim
    0

    Here's a very detailed thread about this on Stack Overflow along with some work arounds :)

  • Kieron McIntyre 110 posts 348 karma points
    Jun 23, 2015 @ 12:45
    Kieron McIntyre
    0

    Yeah, I saw that and have tried them with no luck. But it's also a year old and the issue Shazwazza refers to has been closed and should have been implemented in Umbraco since 7.1.5

    Have you ever had this issue? You must have implemented an async custom controller at some point.

  • Kieron McIntyre 110 posts 348 karma points
    Jun 23, 2015 @ 12:50
  • Tim 1193 posts 2655 karma points MVP c-trib
    Jun 23, 2015 @ 13:00
    Tim
    0

    Hmmmmm, odd. I've done some Async stuff, but not Umbraco Controllers though. I'll ask around the office and see if any of the guys here have tried this.

  • Bunnynut 129 posts 311 karma points
    Jun 30, 2015 @ 06:25
    Bunnynut
    0

    Any news on this issue, because i am running into the same issue.

    In my situation i want to call an action using ajax to create a log record when a user does some actions on the page.

  • Kieron McIntyre 110 posts 348 karma points
    Jun 30, 2015 @ 08:45
    Kieron McIntyre
    0

    Hi @Bunnynut, what you want to do will work fine because you can use a surface controller to handle the submit and these handle async operations fine.

  • Kieron McIntyre 110 posts 348 karma points
    Nov 12, 2015 @ 11:11
    Kieron McIntyre
    1

    I discovered why this wasn't working and it was/is the way Umbraco handles actions in it's RenderActionInvoker class. I've outlined the cause and solutions in these articles: part 1 and part 2.

    But in a nutshell the easiest fix is to target the template by naming the action the same as the template alias. Otherwise you need to register your own controller factory, which is easy enough and detailed in the articles.

  • Norbert Haberl 32 posts 115 karma points
    Aug 20, 2017 @ 12:13
    Norbert Haberl
    0

    Hey, is there any final solution to this? As I can figure out the PR with implementing a separate is canceled.

    So what do we have to do to be capable of using async within Umbraco in

    • Hijacked routes
    • surface controllers
    • WebApi controllers

    Thanks a lot!

  • Kieron McIntyre 110 posts 348 karma points
    Aug 20, 2017 @ 16:34
    Kieron McIntyre
    1

    I'm pretty sure that the latest versions of Umbraco handle async actions in the same way that native MVC does as the controller factory/action invoker gubbins was all simplified to use the underlying MVC logic. So hijacked routes and webapi controllers should handle async fine. However, child actions cannot be async as this isn't possible in MVC anyway (AFAIK).

Please Sign in or register to post replies

Write your reply to:

Draft