Wednesday, 17 March 2021

MVC - How to call async method from controller

 

Watch this example on YouTube

 

 

 

1. Task

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

namespace ValidateOnDbSide.Controllers
{
    public class BusinessController : Controller
    {
        public async Task<string> DoSomething()
        {
           return await Task.Run(() => test());
        }
        public string test()
        {
            return "test";
        }        
    }
}

2. Controller


using System.Threading.Tasks;

namespace ValidateOnDbSide.Controllers
{
    public class HomeController : Controller
    {
        CompanyEntities db = new CompanyEntities();

       

        public async Task<ActionResult> Index()
        {
            BusinessController b = new BusinessController();
            //Task c = b.DoSomething();
            string res = await b.DoSomething();

            return View();
        }

No comments:

Post a Comment