c# - 对异步 mvc Controller 的异步 jquery 调用

标签 c# asp.net-mvc asynchronous

我如何让我的 jquery 方法调用我的 mvc Controller 和我的 mvc Controller 同时做两件事?

jquery 代码没有问题。它只是调用方法并按照我的意愿继续进行。

 $(document).ready(function () {
    console.log("1");
    getADsad();
    console.log("2");
    lala();
    console.log("3");
});
    function getADsad() {
        $.ajax({
            url: '/Configurator/Configure/Hello1',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                console.log(data + "hello1");
            }
        });
    }

function lala() {
    $.ajax({
        url: '/Configurator/Configure/Hello2',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            console.log(data + "hello2");
        }
    });

另一方面,我的 C# 代码并没有同时做两件事:

    [HttpPost]
    public async Task<LoginViewModel> Hello1()
    {
        var str = await GetSlowstring();
        return str;
    }

    [HttpPost]
    public async Task<LoginViewModel> Hello2()
    {
        var str = await GetSlowstring();
        return str;
    }

    public async Task<LoginViewModel> GetSlowstring()
    {
        await Task.Delay(10000);
        LoginViewModel login = new LoginViewModel();
        login.UserName = "HejsN";
        return await Task.FromResult(login);
    }

如果正确完成,组合调用应该只需要 10 秒多一点,但现在需要两倍。

我需要为调用创建一个新线程吗?还是由应用程序池自动完成?

编辑: enter image description here

最佳答案

根据 Chrome 控制台的图像,问题出在 SessionState 中。默认情况下,当应用程序使用 SessionState 时,ASP 框架会连续处理来自单个用户的请求(以防止 session 变量受到潜在损坏)。

对于某些情况,当您想要为单个用户启用并行处理请求(并且不需要更新 session )时,您可以在 Controller 上使用 SessionState 属性。

 [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

更多详细信息可以在这里找到,例如:http://www.stefanprodan.com/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/

关于c# - 对异步 mvc Controller 的异步 jquery 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643590/

相关文章:

c# - 如何拆分和合并此数据流管道?

c# - 找不到 Visual Studio 2015 上的 ASP.NET 5 addMvc 方法

ajax - 在 OnSuccess 处理程序中获取对 Ajax.ActionLink 的 anchor 元素的引用

asp.net-mvc - System.Web.HttpException (0x80004005) : Maximum request length exceeded

c# - SQLite问题“无法打开数据库文件”

Swift 4. 在继续执行之前等待 HealthKit HKQuery 的异步结果

c# - MvvmCross 命令中的异步任务未返回

c# - 具有相同 ID 的多个控件

c# - 在.NET中,是否需要注册DLL?

c# - Xamarin 无法将类型隐式转换为 xamarin.Forms.Page