為什么響應(yīng).重定向?qū)е耂ystem.Thread.ThreadAbortException?當(dāng)我使用Response.重定向(.)要將表單重定向到新頁面,我將得到以下錯誤:類型‘System.Threading.ThreadAbortException’的第一次機(jī)會異常發(fā)生在mcorlib.dll中。類型‘System.Threading.ThreadAbortException’的異常發(fā)生在m336 lib.dll中,但未在用戶代碼中處理。我對此的理解是,錯誤是由Web服務(wù)器中止調(diào)用response.redirected頁面的其余部分引起的。我知道我可以在Response.Redirect這叫做endResponse。如果我將endResponse設(shè)置為True,我仍然會得到錯誤,但如果我將其設(shè)置為false,則不會。我很確定,盡管這意味著Web服務(wù)器正在運(yùn)行我重定向的頁面的其余部分。至少可以說這是沒有效率的。有更好的方法嗎?其他的東西Response.Redirect或者,是否有一種方法可以強(qiáng)制舊頁停止加載我無法獲得ThreadAbortException?
3 回答

慕碼人2483693
TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊
Response.Redirect
Try...Catch
編輯
這段代碼很不完美,可能不應(yīng)該被復(fù)制,因為我還沒有測試過它。
public void btnLogin_Click(UserLoginViewModel model){ bool ValidLogin = false; // this is our "result value" try { using (Context Db = new Context) { User User = new User(); if (String.IsNullOrEmpty(model.EmailAddress)) ValidLogin = false; // no email address was entered else User = Db.FirstOrDefault(x => x.EmailAddress == model.EmailAddress); if (User != null && User.PasswordHash == Hashing.CreateHash(model.Password)) ValidLogin = true; // login succeeded } } catch (Exception ex) { throw ex; // something went wrong so throw an error } if (ValidLogin) { GenerateCookie(User); Response.Redirect("~/Members/Default.aspx"); } else { // do something to indicate that the login failed. }}
- 3 回答
- 0 關(guān)注
- 295 瀏覽
添加回答
舉報
0/150
提交
取消