ASP.Net MVC中长时间运行服务器调用的进度条
发布时间:2020-05-23 18:08:29 所属栏目:asp.Net 来源:互联网
导读:我只想在长时间运行服务器调用时创建一个进度条。当控制器执行长时间运行的作业时,我无法在控制器上创建ajax发布请求。 我想创建一个额外的操作来获取当前长时间运行的任务的实际语句。 我试图在ajax请求中创建poll,然后我可以从服务器端返回状态并将其显示
|
我只想在长时间运行服务器调用时创建一个进度条。当控制器执行长时间运行的作业时,我无法在控制器上创建ajax发布请求。 我想创建一个额外的操作来获取当前长时间运行的任务的实际语句。 解决方法使用SignalR,最合适的方法就是做到这一点。请在 https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2下载Microsoft SignalR在名为集线器的项目路径中的单独文件夹中创建集线器类,将两个类文件添加到集线器文件夹中 Startup.cs 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();
}
}
}
ProgressHub.cs 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);
}
}
}
在控制器中, // assemblies
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;
//call this method inside your working action
ProgressHub.SendMessage("initializing and preparing",2);
在视图中, <!--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>
有关详细信息,请参阅Google的帮助下的一些现有文章 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net核心中的Request.CreateResponse
- .net – 在控制器中查看列表数据
- asp-classic – 如何使用经典asp中的vbscript下载文件
- asp.net-mvc – 如何告诉Ninject绑定到它没有引用的实现
- 从客户端调用asp.net ajax服务器控件的公共函数
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net-mvc-4 – ViewModels或ViewBag?
- asp.net – 如何在Windows Phone中开发Google地图
- asp.net-mvc – ASP MVC查看内容为JSON
- asp.net – UserControl Viewstate在回发后丢失所有值
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC jQueryUI datepicke
- asp.net – 此操作需要连接到“主”数据库
- asp.net-mvc – RouteValueDictionary的字符串UR
- asp.net – .NET“代码块块”?
- asp.net – 自定义控件变为通用的“UserControl”
- 模型 – 视图 – 控制器 – ASP.NET MVC:使用Se
- 如何使用ASP.NET Core创建多部分HTTP响应
- asp.net-mvc – 是否可以使用RedirectToAction清
- asp.net – 如何使Owin自主主机支持Json输出?
- 如何删除IIS自定义标头像X-Powered-By:ASP.NET从
热点阅读
