我有一个测验应用程序,它使用以下方法随机选择 5 个页面之一:
protected void newWindow(object sender, EventArgs e)
    {
        int next = new Random().Next( 5 ) + 1;
        Response.Redirect(string.Format( "Question{0}.aspx", next ));
    }
如何防止该方法访问已访问的页面?
像这样的东西(未经测试)
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、2、15、12 之类的 csv 维护会话变量,并根据这些值检查下一个变量。如果会话中存在它,则再次掷骰子,否则显示页面并将当前的旁边附加到会话变量。
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 ));
}