2014-02-11 60 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