如何在 ASP.NET WebForms 中安排任务在特定日期之后执行?
假设我正在设计一个考试门户页面,我想在其中显示考试日期和时间的倒计时,如果到达特定日期和时间,我想将用户重定向到登录页面。我该怎么做?我尝试过使用 Hangfire,但在到达时间后它不会将我重定向到重定向页面。计划的作业会在数据库 (HangFire.Job) 上更新,但它不会将我重定向到其他页面。我是编程方面的新手,我知道的不多,所以指导我如何实现类似的事情将非常感激,是的,是否有可能使用 Hangfire 做这样的事情?
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
GlobalConfiguration.Configuration
.UseSqlServerStorage("calanders");
var option = new DashboardOptions { AppPath = VirtualPathUtility.ToAbsolute("/Default.aspx") };
app.UseHangfireDashboard("/hangfire",option);
app.UseHangfireServer();
}
这是 Startup.cs
protected void Button1_Click(object sender, EventArgs e)
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("calanders", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true
});
BackgroundJob.Schedule(() => startCountdown(),TimeSpan.FromSeconds(20));
//d.InsertDate(TextBox1.Text.ToDa);
}
public void startCountdown()
{
Response.Write("<script>alert('Time Reached')</script>");
Response.Redirect("WebForm1.aspx");
}
这是主页。 (默认.aspx)
Say I am desingning an exam portal page where i want To show countdown for the exam date and time and if that specific date and time is reached I want to redirect user to a login page. How can i do this? I have tried using Hangfire but it doesnt redirect me to the redirected page after the time is reached.The scheduled jobs gets updated on the database (HangFire.Job) but it doesnt redirect me to the other page. I am a total newbie in programming and i dont know much so Guiding me on how to acheive something like that would be appreciated a lot and yes, is it even possible to do something like this using Hangfire ?
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
GlobalConfiguration.Configuration
.UseSqlServerStorage("calanders");
var option = new DashboardOptions { AppPath = VirtualPathUtility.ToAbsolute("/Default.aspx") };
app.UseHangfireDashboard("/hangfire",option);
app.UseHangfireServer();
}
This is the Startup.cs
protected void Button1_Click(object sender, EventArgs e)
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("calanders", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true
});
BackgroundJob.Schedule(() => startCountdown(),TimeSpan.FromSeconds(20));
//d.InsertDate(TextBox1.Text.ToDa);
}
public void startCountdown()
{
Response.Write("<script>alert('Time Reached')</script>");
Response.Redirect("WebForm1.aspx");
}
This is the Main Page. (Default.aspx)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Web 表单中,在回发之间没有与服务器的持续连接,因此在服务器端激活的作业将无法在任意未来时间与用户交互;仅在触发按钮单击和返回响应之间的时间内。
如果您想显示一个计时器,然后在完成后执行某些操作,您应该研究如何在客户端使用 Javascript 来执行此操作(如果这适用于您的场景)。如果需要,您可以使用 Javascript 向服务器发出后台请求,但您的工作不会直接由服务器端驱动。
In webforms there is no ongoing connection to the server in between postbacks, so a job that is activated on the server side won't be able to interact with the user at an arbitrary future time; only during the immediate time between the button click being triggered and the response being returned.
If you want to show a timer and then do something when it is done, you should look into how to do it with Javascript on the client side, if that works for your scenario. You can use Javascript to make background requests to the server if you need to, but your job won't be directly driven by the server side.