0

I want to call async method inside the ActionResult. This is how my code looks like

public HomeController :  GlassController
{
     public override Task<ActionResult> Index()
      {
         //code
         vm.parkData=await _parkService.GetSpots(siteId);
         //code
      }
}

Since GetSpots is an async method so I have added the await operator and I change ActionReult from public override ActionResult Index() to public override async Task<ActionResult> Index() to support the async method

But I am getting an error

'HomeController.Index()': return type must be 'ActionResult' to match overridden member 'SitecoreController.Index()'

Since my HomeController is inheriting GlassController and GlassController is inheriting SitecoreContoller. The SitecoreController does not have any async index method so I am getting the error.

Is there any way I can call the GetSports async method inside the index Action Index() method

Any help or suggestions would be appreciated

Thanks in advance

2

Sitecore does not support Async controller rendering. but from the Sitecore 8.2 it supports async MVC controllers.

If you want to use async actions, you need to use it outside of Sitecore contexts by indirectly invoked controllers via non-Sitecore routes or Html.RenderAction() on a Sitecore rendering. one other option is to use the AJAX base on your requirements.

0

You can not use async pattern in Sitecore as it does not support it. If you will try to make it work, you will get exceptions or it will work synchronously. There is a very good article from kamsar on this. Please go through it to understand better. A quick excerpt from the article -

Looking around in the ASP.NET MVC source code, you can see that there are two interfaces for action invokers: IActionInvoker and IAsyncActionInvoker. Guess what? Sitecore’s ActionInvoker implements IActionInvoker but NOT IAsyncActionInvoker - which means that even though the default MVC action invoker it was wrapping supported async invocation, the wrapper’s lack of IAsyncActionInvoker implementation meant ASP.NET MVC wouldn’t use async invocation at all - and would instead throw an error that you cannot return a Task from a synchronous controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy