![](/img/trans.png)
[英]Redirecting to another page using C# Response.Redirect in javascript
[英]How to redirect htm page to another using Response.Redirect?
我正在嘗試將我的 login.htm 重定向到 c# 中的 index.htm。 這兩個文件都在名為“default”的文件夾中。 登錄后,如果憑據正確,我想將登錄頁面重定向到 index.html 。 使用下面的代碼,但有一條異常消息Attempted to cancel thread.
. 有沒有辦法來解決這個問題?? 你怎么能幫我。 提前致謝。
[WebMethod]
public void LogMeIn(string user, string pass) {
try {
using (MySqlConnection dbConn = new MySqlConnection(connectionString())) {
if (dbConn.State == System.Data.ConnectionState.Open) dbConn.Close();
dbConn.Open();
MySqlCommand sqlCmd = new MySqlCommand("SELECT * FROM tbllogin WHERE userName = '" + user + "' AND passWord ='" + pass + "'", dbConn);
int rowCount = (int)sqlCmd.ExecuteScalar();
if (rowCount > 0) {
HttpContext.Current.Response.Redirect("../default/index.htm"); //redirect to new htm page
}
}
}
catch (Exception ex) {
Console.WriteLine("Error Message : " + ex.Message);
}
}
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath+"default/index.htm");
或者你可以使用這種方式。
HttpContext.Current.Response.Redirect 試圖將用戶發送到錯誤的地方
HttpContext.Current.Response.Redirect("~/default/index.htm");
您的代碼示例中有一些錯誤和不必要的代碼:
dbConn
是否已經打開,因為它是剛剛創建的SELECT *
在稍后執行ExecuteScalar
時導致異常,因為該表有多個列。 您應該將其更改為SELECT COUNT(*)
Response.Redirect
時沒有結束Response.Redirect
您可能需要捕獲ThreadAbortException
。 要結束響應,您可以檢查Redirect(string url, bool endResponse) 重載話雖如此,您應該使用參數化查詢,因為意外或惡意輸入可能會給您帶來問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.