2014-02-11 62 views
-1

,我发现了以下错误:HTTP 404错误与Response.Redirect的

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable

用下面的代码:

SqlConnection cn = new SqlConnection(@"Data Source=bpnaidu-pc\sqlexpress;Initial Catalog=UserLogIn;Integrated Security=True;Pooling=False"); 

cn.Open(); 
SqlCommand cmd = new SqlCommand("INSERT INTO regd values('" + ddl_signup.Text + "','" + txt_user.Text + "','" + txtEmail1.Text+ "','" + txtPassword.Text + "','" + txtConPassword.Text + "')", cn); 
cmd.ExecuteNonQuery(); 
Response.Redirect("welcome to "+txt_user.Text.ToString()); 
Response.Redirect("Home.aspx"); 
cn.Close(); 

问题出在哪里?

回答

0

虽然问题并不直接明确,但答案是肯定的。问题是,在这条线:

Response.Redirect("welcome to "+txt_user.Text.ToString()); 

的Response.Redirect将浏览器的参数中指定的网页,所以你要浏览器发送到一个名为页“欢迎来到{...}” 。我觉得这不太可能。我怀疑你真正想要的是:

Response.Write("welcome to "+txt_user.Text.ToString()); 

Response.Write只是直接向HTTP输出缓冲区发送一个文本字符串。

但是,如果您立即将此信息重定向到Home.aspx,那么实际上这条线实际上什么都不做,因为您刚写完输出,而您告诉浏览器重定向到不同的页面,因此用户不会看到你的欢迎信息。

我还想指出的是,你打开一个SQL注入攻击,以及 - 你行:

SqlCommand cmd = new SqlCommand("INSERT INTO regd values('" + ddl_signup.Text + "','" + txt_user.Text + "','" + txtEmail1.Text+ "','" + txtPassword.Text + "','" + txtConPassword.Text + "')", cn); 

...是对用户公开做坏事;如果txt_user.Text包含文本'); drop table regd;,那么很有可能最终会丢失regd表。要修复,你需要使用参数化的SQL语句。我不强烈建议您阅读Jeff Atwood的blog article about this以及如何缓解。

+0

是的,这个Responce.Write工作。谢谢回复... – Sankar