Async Task <actionresult> vs AsyncTask MVC Controller for Long operations

时间:2017-04-24 13:49:47

标签: c# asp.net-mvc model-view-controller

使用Ajax调用对公共MVC方法的调用。

    Dart.LoadProfile.DetailProfileLoad = function (url) {
    $.ajax({
        url: url,
        type: 'POST',
        data: null,
        xhrFields: {
            withCredentials: true,
        },
        success: function (data) {
            //get JSON data from response
            jQuery.each(data, function (index, itemData) {
                //Code here to display result
            });
        },
        error: function (jqXHR, ajaxSettings, thrownError) {
            console.log(thrownError);
        },
    });

};
  1. 非异步

        [HttpPost]
    [AllowAnonymous]
    public ActionResult GeneralLoadProfileResult(long fid, long uniqueId, string loadyear)
    {
        try
        {     
            //Code here. Average 1 - 1.5 min to compelte calculation 
    
           return Json(new { result = txLoadReport });
        }
        catch (Exception)
        {
            throw;
        }
    }
    
  2. 异步

        [HttpPost]
    [AllowAnonymous]
    public async Task<ActionResult> GeneralLoadProfileResult(long fid, long uniqueId, string loadyear)
    {
        try
        {
    
            return await Task.Run<ActionResult>(() =>
            {
                //Code here. Average 1 - 1.5 min to compelte calculation
    
                return Json(new { result = txLoadReport });
    
            });
    
        }
    
        catch (Exception)
        {
            throw;
        }
    
    }
    
  3. 有人可以解释一下上面提供的样本中使用Async(如果有的话)的好处吗?进行异步与非异步调用会更有效率,即当多个用户同时发出相同的请求时?

    谢谢。

0 个答案:

没有答案