我有一个测验应用程序,可以使用以下方法随机选择5个页面中的一个:
protected void newWindow(object sender, EventArgs e)
{
int next = new Random().Next( 5 ) + 1;
Response.Redirect(string.Format( "Question{0}.aspx", next ));
}
如何阻止该方法访问已访问过的网页?
答案 0 :(得分:2)
像这样(未经测试)
protected void newWindow(object sender, EventArgs e)
{
List<int> questions = (List<int>)Session["Questions"];
if (questions == null)
{
questions = new List<int>(new int[] { 1, 2, 3, 4, 5 });
}
int nextIndex = new Random().Next(questions.Count());
int next = questions[nextIndex];
questions.RemoveAt(nextIndex);
Session["Questions"] = questions;
Response.Redirect(string.Format( "Question{0}.aspx", next ));
}
答案 1 :(得分:0)
使用类似1,2,15,12的csv维护会话变量,并根据这些值检查下一个变量。如果它存在于会话滚动中,则骰子再次显示页面并将当前附加到会话变量旁边。
答案 2 :(得分:0)
using System.Linq;
protected void newWindow(object sender, EventArgs e)
{
var pagesVisited = (List<int>)Session["Visited"] ?? new List<int>() { 1, 2, 3, 4, 5 };
if (!pagesVisited.Any())
// the user has visited all quizes
var index = new Random().Next(0, pagesVisited.Count)
var next = index + 1;
pagesVisited.RemoveAt(index);
Session["Visited"] = pagesVisited;
Response.Redirect(string.Format( "Question{0}.aspx", next ));
}