是否可以使用razor页面、非MVC页面、直接razor页面更新引导进度条的百分比
我在asp.net core 3.1 razor页面web应用程序上有一个长时间运行的进程,这是非MVC重复,不是MVC设置。。我可以在razor页面上用c#code back运行一个长时间运行的进程时,显示一个引导进度条,它可以无限滚动 我正在寻找一种方法,可以在cshtml.cs代码中的c#中执行的长时间运行任务的代码/函数中的特定点更新引导进度条百分比 我发现的所有东西都是面向MVC环境的,我尝试移植了一种MVC方法,但没有成功,只是想知道是否有人只使用纯Razor页面就找到了一种从代码背后更新进度条的解决方案?或者,如果代码隐藏无法实现,则使用客户端javascript(或我不太熟悉的ajax)检查代码隐藏上的任何绑定属性/变量(如果我更新了它们,例如百分比更改变量;)或者类似的东西 [更新] 我试图遵循的博客示例 我想出了这个剃须刀页面代码,但它不起作用,哈哈 html是否可以使用razor页面、非MVC页面、直接razor页面更新引导进度条的百分比,razor,razor-pages,Razor,Razor Pages,我在asp.net core 3.1 razor页面web应用程序上有一个长时间运行的进程,这是非MVC重复,不是MVC设置。。我可以在razor页面上用c#code back运行一个长时间运行的进程时,显示一个引导进度条,它可以无限滚动 我正在寻找一种方法,可以在cshtml.cs代码中的c#中执行的长时间运行任务的代码/函数中的特定点更新引导进度条百分比 我发现的所有东西都是面向MVC环境的,我尝试移植了一种MVC方法,但没有成功,只是想知道是否有人只使用纯Razor页面就找到了一种从代码背
启动后台进程
单击下面的按钮,启动长时间运行的流程:
队列后台作业
您的背景工作状态:工作状态将显示在此处…
@节脚本
{
var connection=new signal.HubConnectionBuilder()
.withUrl(“/jobprogress”)
.configureLogging(信号器.LogLevel.Information)
.build();
连接。关于(“进度”,
(百分比)=>{
如果(百分比===100){
document.getElementById(“作业状态”).innerText=“完成!”;
}否则{
document.getElementById(“作业状态”).innerText=`${percent}%`;
}
});
connection.start()
.then(=>connection.invoke(“AssociateJob”,“@Model.JobId”))
.catch(err=>console.error(err.toString());
}
代码隐藏
[BindProperties (SupportsGet = true)]
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IQueue _queue;
private readonly IHubContext<JobProgressHub> _hubContext;
public string JobId { get; set; }
public IndexModel(ILogger<IndexModel> logger, IQueue queue, IHubContext<JobProgressHub> hubContext)
{
_logger = logger;
_queue = queue;
_hubContext = hubContext;
}
public IActionResult OnGet()
{
JobId = HttpContext.Session.GetString("jobId");
return Page();
}
public IActionResult OnPost()
{
string jobId = Guid.NewGuid().ToString("N");
_queue.QueueAsyncTask(() => PerformBackgroundJob(jobId));
HttpContext.Session.SetString("jobId", jobId);
JobId = jobId;
return Page();
}
private async Task PerformBackgroundJob(string jobId)
{
for (int i = 0; i <= 100; i += 5)
{
// TODO: report progress with SignalR
await _hubContext.Clients.Group(jobId).SendAsync("progress", i);
await Task.Delay(100);
}
}
}
[BindProperties(SupportsGet=true)]
公共类索引模型:PageModel
{
专用只读ILogger\u记录器;
专用只读IQueue_队列;
私有只读IHubContext\u hubContext;
公共字符串JobId{get;set;}
公共索引模型(ILogger记录器、IQueue队列、IHubContext hubContext)
{
_记录器=记录器;
_队列=队列;
_hubContext=hubContext;
}
公共IActionResult OnGet()
{
JobId=HttpContext.Session.GetString(“JobId”);
返回页();
}
public IActionResult OnPost()
{
字符串jobId=Guid.NewGuid().ToString(“N”);
_queue.QueueAsyncTask(()=>PerformBackgroundJob(jobId));
HttpContext.Session.SetString(“jobId”,jobId);
JobId=JobId;
返回页();
}
专用异步任务PerformBackgroundJob(字符串jobId)
{
对于(int i=0;i
[BindProperties (SupportsGet = true)]
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IQueue _queue;
private readonly IHubContext<JobProgressHub> _hubContext;
public string JobId { get; set; }
public IndexModel(ILogger<IndexModel> logger, IQueue queue, IHubContext<JobProgressHub> hubContext)
{
_logger = logger;
_queue = queue;
_hubContext = hubContext;
}
public IActionResult OnGet()
{
JobId = HttpContext.Session.GetString("jobId");
return Page();
}
public IActionResult OnPost()
{
string jobId = Guid.NewGuid().ToString("N");
_queue.QueueAsyncTask(() => PerformBackgroundJob(jobId));
HttpContext.Session.SetString("jobId", jobId);
JobId = jobId;
return Page();
}
private async Task PerformBackgroundJob(string jobId)
{
for (int i = 0; i <= 100; i += 5)
{
// TODO: report progress with SignalR
await _hubContext.Clients.Group(jobId).SendAsync("progress", i);
await Task.Delay(100);
}
}
}