在应用程序中有 2 个页面,CompletePoll.aspx、Default.aspx。
CompletePoll.aspx --> Page_Load()
Ultoo u = new Ultoo();
u.UserName = Request["username"].ToString();
u.Password = Request["password"].ToString();
new Thread(u.CompletePoll).Start();
完成投票()
.......
.......
String str = "Question:" + QuestionGenerator.GetNextQuestion(); /*Here i am getting Type initializer exception*/
.......
.......
问题生成器
public static class QuestionGenerator
{
private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
private static string[] SecondParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart2.txt")).ReadToEnd(). Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
private static Random r = new Random();
public static string GetNextQuestion()
{
return FirstParts[r.Next(0, FirstParts.Length - 1)] + " " + SecondParts[r.Next(0, SecondParts.Length - 1)] + "?";
}
}
但是,如果我先调用 Default.aspx,然后调用 CompletePoll.aspx,则代码工作正常。
Default.aspx --> Page_Load()
Label1.Text = QuestionGenerator.GetNextQuestion();
所以这里我的问题是,如果我首先访问 CompletePoll.aspx ,我会得到 TypeInitializer 异常。如果我先访问 Default.aspx,然后再访问 CompletePoll.aspx,我不会遇到任何问题。我的代码有什么问题,我错过了什么吗?如何首先访问 CompletePoll.aspx?