在系统登陆界面中Response.Redirect()函数出现奇怪的现象。开始时,系统未连接数据库,本人用XML形式测试代码,如下:
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("") + @"\database.xml");
XmlElement root = doc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
if (Login1.UserName == node.Attributes["name"].Value && Login1.Password == node.Attributes["pwd"].Value)
{
Session["userName"] = Login1.UserName;
Session["authority"] = node.Attributes["authority"].Value;
Response.Redirect(Login1.DestinationPageUrl, true); //验证成功,xml测试时成功跳转
}
}
Response.Redirect("Default.aspx"); //身份验证未通过,跳转到原页面其中的database.xml中包含了登陆用户,Login1.DestinationPageUrl属性包含了身份验证通过后的主页面Main.aspx。
Response.Redirect(Login1.DestinationPageUrl, true);
如果对Response.Redirect()进行try...catch...出现ThreadAbortException异常,这是正常线程结束异常,无关紧要。后来,系统连接到数据库后的代码如下:
string userID = Login1.UserName.Trim();
string password = Login1.Password.Trim();
string strSelect = "select OperatorID,OperatorPass,OperatorName,OperatorType,FieldCode from pb_Operator where OperatorID='" + userID + "' and OperatorPass='" + password + "'"; //数据库查询字符串
SqlDataReader reader = dbManager.GetDataReader(strSelect);
if (reader.Read()) //数据库查询通过
{
//保存到Session
Session["OperatorID"] = reader["OperatorID"]; //帐号
Session["OperatorName"] = reader["OperatorName"]; //姓名
Session["OperatorType"] = reader["OperatorType"]; //操作员类型
Session["FieldCode"] = reader["FieldCode"]; //所属域编号
Response.Redirect(Login1.DestinationPageUrl, true); //系统也成功运行到这里,但是却跳转到原页面Default.aspx,而不是指定的Main.aspx页面。这是为什么?
}
Response.Redirect("Default.aspx");数据库连接成功,并且也成功读取了用户数据并保存到Session变量中。但是到了Response.Redirect(Login1.DestinationPageUrl, true);后却不进行页面跳转(页面也进行了跳转,但是却跳转到Default.aspx,而不是指定的Main.aspx),还是在原来的Default.aspx登陆页面。进行try...catch...出现同样的ThreadAbortException异常。如此这般,就是改变Login1.DestinationPageUrl的跳转页面值,也还是回到原来的Default.aspx页面,甚至在跳转页面值指向其他文件夹中的页面如xxx\xxx.aspx,系统会跳出xxx\Default.aspx页面不存在的错误。
也就是说,好像系统绑定了Default.aspx似的。因此,我在清空了系统Frame文件夹下的临时文件。原以为总不会出现了,但是还是出现一直Default.aspx的现象。我都快被逼疯了,高手来救救我吧。
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("") + @"\database.xml");
XmlElement root = doc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
if (Login1.UserName == node.Attributes["name"].Value && Login1.Password == node.Attributes["pwd"].Value)
{
Session["userName"] = Login1.UserName;
Session["authority"] = node.Attributes["authority"].Value;
Response.Redirect(Login1.DestinationPageUrl, true); //验证成功,xml测试时成功跳转
}
}
Response.Redirect("Default.aspx"); //身份验证未通过,跳转到原页面其中的database.xml中包含了登陆用户,Login1.DestinationPageUrl属性包含了身份验证通过后的主页面Main.aspx。
Response.Redirect(Login1.DestinationPageUrl, true);
如果对Response.Redirect()进行try...catch...出现ThreadAbortException异常,这是正常线程结束异常,无关紧要。后来,系统连接到数据库后的代码如下:
string userID = Login1.UserName.Trim();
string password = Login1.Password.Trim();
string strSelect = "select OperatorID,OperatorPass,OperatorName,OperatorType,FieldCode from pb_Operator where OperatorID='" + userID + "' and OperatorPass='" + password + "'"; //数据库查询字符串
SqlDataReader reader = dbManager.GetDataReader(strSelect);
if (reader.Read()) //数据库查询通过
{
//保存到Session
Session["OperatorID"] = reader["OperatorID"]; //帐号
Session["OperatorName"] = reader["OperatorName"]; //姓名
Session["OperatorType"] = reader["OperatorType"]; //操作员类型
Session["FieldCode"] = reader["FieldCode"]; //所属域编号
Response.Redirect(Login1.DestinationPageUrl, true); //系统也成功运行到这里,但是却跳转到原页面Default.aspx,而不是指定的Main.aspx页面。这是为什么?
}
Response.Redirect("Default.aspx");数据库连接成功,并且也成功读取了用户数据并保存到Session变量中。但是到了Response.Redirect(Login1.DestinationPageUrl, true);后却不进行页面跳转(页面也进行了跳转,但是却跳转到Default.aspx,而不是指定的Main.aspx),还是在原来的Default.aspx登陆页面。进行try...catch...出现同样的ThreadAbortException异常。如此这般,就是改变Login1.DestinationPageUrl的跳转页面值,也还是回到原来的Default.aspx页面,甚至在跳转页面值指向其他文件夹中的页面如xxx\xxx.aspx,系统会跳出xxx\Default.aspx页面不存在的错误。
也就是说,好像系统绑定了Default.aspx似的。因此,我在清空了系统Frame文件夹下的临时文件。原以为总不会出现了,但是还是出现一直Default.aspx的现象。我都快被逼疯了,高手来救救我吧。
可能根本就没有执行到Response.Redirect
{
//保存到Session
Session["OperatorID"] = reader["OperatorID"]; //帐号
Session["OperatorName"] = reader["OperatorName"]; //姓名
Session["OperatorType"] = reader["OperatorType"]; //操作员类型
Session["FieldCode"] = reader["FieldCode"]; //所属域编号
Response.Redirect(Login1.DestinationPageUrl, true); //系统也成功运行到这里,但是却跳转到原页面Default.aspx,而不是指定的Main.aspx页面。这是为什么?
}
else
Response.Redirect("Default.aspx");
我不喜欢写程序的逻辑故意不明白、靠“隐喻”让读者去揣摩,绝对不能说明程序员更懂技术。应该写出大实话般的程序。
{
//保存到Session
Session["OperatorID"] = reader["OperatorID"]; //帐号
Session["OperatorName"] = reader["OperatorName"]; //姓名
Session["OperatorType"] = reader["OperatorType"]; //操作员类型
Session["FieldCode"] = reader["FieldCode"]; //所属域编号
Response.Redirect(Login1.DestinationPageUrl, true); //系统也成功运行到这里,但是却跳转到原页面Default.aspx,而不是指定的Main.aspx页面。这是为什么?
}
else
{
Response.Redirect("Default.aspx");
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
if (reader.Read()) //数据库查询通过
{
//保存到Session
Session["OperatorID"] = reader["OperatorID"]; //帐号
Session["OperatorName"] = reader["OperatorName"]; //姓名
Session["OperatorType"] = reader["OperatorType"]; //操作员类型
Session["FieldCode"] = reader["FieldCode"]; //所属域编号
Response.Redirect(Login1.DestinationPageUrl, true); //系统也成功运行到这里,但是却跳转到原页面Default.aspx,而不是指定的Main.aspx页面。这是为什么?
}
else
{
Response.Redirect("Default.aspx");
}
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
是不是因为什么条件跳转了?
Login1.DestinationPageUrl的值就是目标页面,正确。程序是发生了跳转,但是却跳转到原Default.aspx页面上,而不是目标页面。
reader.Read(),根据其中是否有值来判断用户身份验证是否通过。
这些都是没问题的,问题就是程序到达了Response.Redirect()函数,为什么没有跳转到Login1.DestinationPageUrl指定的页面?
更让人想不通的是,在xml测试时没问题,在数据库测试却出现这种问题。把这两份代码反复注释掉其中一份,还是出现xml测试没问题,数据库测试有问题。天呐,这不是耍我吗?
.net有时候就是会出点稀奇的问题,如果你觉得实在是逻辑正确的话!那么给你点经验
解决方法:项目重新编译 若不行
则重起计算机 重新编译