asp.net - ASP.Net MVC 中长时间运行的服务器调用的进度条

标签 asp.net ajax asp.net-mvc

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




我只想在长时间运行服务器调用时创建一个进度条。当 Controller 正在执行长时间运行的工作时,我无法向 Controller 创建 ajax 发布请求。

我想创建一个额外的 Action 来获取当前长时间运行任务的实际语句。
我尝试在 ajax 请求中创建轮询,然后我可以从服务器端返回状态并将其显示在客户端进度条中。有任何想法吗 ?

最佳答案

正确且最简单的方法是使用 SignalR。请在 https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2 下载 Microsoft SignalR

在名为 hubs 的项目路径中的单独文件夹中创建一个 hub 类,将两个类文件添加到 hubs 文件夹中

启动文件

using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

进度中心
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace RealTimeProgressBar
{
    public class ProgressHub : Hub
    {

        public string msg = "Initializing and Preparing...";
        public int count = 1;

        public static void SendMessage(string msg , int count)
        {
            var message = "Process completed for " + msg;
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
            hubContext.Clients.All.sendMessage(string.Format(message), count);
        }

        public void GetCountAndMessage()
        {
            Clients.Caller.sendMessage(string.Format(msg), count);
        }
    }
}

在 Controller 中,
// assemblies
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;   


//call this method inside your working action
ProgressHub.SendMessage("initializing and preparing",  2);

在 View 中,
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.--> 
<script type="text/javascript">
  $(document).ready(function () {

    $("#progressBar").kendoProgressBar({
        min: 0,
        max: 100,
        type: "percent",
    });
});

function StartInvoicing()
{
    var progressNotifier = $.connection.progressHub;

    // client-side sendMessage function that will be called from the server-side
    progressNotifier.client.sendMessage = function (message, count) {
        // update progress
        UpdateProgress(message, count);
        //alert(message);
    };

    // establish the connection to the server and start server-side operation
    $.connection.hub.start().done(function () {
        // call the method CallLongOperation defined in the Hub
        progressNotifier.server.getCountAndMessage();
    });
}

// Update the progress bar 
function UpdateProgress(message, count) {
    var result = $("#result");
    result.html(message);
    $("#progressBar").data("kendoProgressBar").value(count);
}
</script>

有关更多详细信息,请在谷歌的帮助下引用一些现有文章

关于asp.net - ASP.Net MVC 中长时间运行的服务器调用的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313550/

相关文章:

jquery - 根据 CSV 中的值检查复选框

标题元素中的 ASP.NET 换行符

asp.net - 如何将 asp.net 应用程序部署到 Linux 服务器上的 docker 容器?

asp.net - HttpMessageHandler 与 DelegatingHandler

c# - 我如何使用 System.Net.NetworkInformation.GatewayIPAddressInformation 类?

c# - 渲染前取消转义字符串中的转义字符

ajax - Symfony2,检查一个 Action 是否被 ajax 调用

javascript - 在非 ajax 帖子中包含 json 数据

javascript - 如何在AJAX中形成授权CURL请求?

asp.net-mvc - 如何在 CSHTML 文件中引用 CSS?