Today I ran into an issue that I hadn’t seen in a long time. I was asked to diagnose two errors. “Cannot Redirect after HTTP headers have been sent”, and some random “Object Reference not Set” errors.
After analyzing some of the logs, I found that they were always found after logging out. Digging further, I found this statement: Response.Redirect(url, false);
For those who might not know, the ‘False’ parameter causes a ThreadAbortException. Why? To make sure your requests ends immediately after that.
At first, I was tempted to ‘just’ change the parameter to ‘true’ and be done with it, but I wanted to know why one of my colleagues made this change. And interestingly enough.. it showed up in the history: “added ‘false’ parameter to prevent ThreadAbortException in the logs”. So, one of my colleagues probably found his logs were full of ThreadAbortExceptions. So he avoided the exceptions and now the logs were full of other, more cryptic errors.
So I figured out the best solution would be to make sure this ThreadAbortException was not being logged. It turns out that this exception already doesn’t reach the Global.asax Application_OnError method. So that wasn’t the source of the logging. it turned out that we had some exception handling logic built ourselves. So, adding a check to see if this was a redirect solved the issue. A little extension method Exception.IsCausedByRedirect() made the code a bit more readable:
1: public partial class _Default : Page
2: {
3: protected void Page_Load(object sender, EventArgs e)
4: {
5:
6: }
7:
8: protected void myButton_OnClick(object sender, EventArgs e)
9: {
10: try
11: {
12: Response.Redirect("About.aspx");
13: }
14: catch (Exception ex)
15: {
16: if (ex.IsCausedByRedirect())
17: {
18: Debug.WriteLine("Redirecting to: " + Response.RedirectLocation);
19: }
20: else
21: {
22: Debug.WriteLine(ex);
23: }
24: throw;
25: }
26: }
27: }
28:
29: public static class ExceptionExtensions
30: {
31: /// <summary>
32: /// Returns if an exception is caused by a redirect. A redirect throws
33: /// a ThreadAbortException (normal behavior to stop execution). However
34: /// this exception typically shouln't be put in the logs.
35: /// </summary>
36: /// <param name="exception">The exception to check</param>
37: /// <returns>True if this exception is caused by a redirect. False if not. </returns>
38: public static bool IsCausedByRedirect(this Exception exception)
39: {
40: return exception is ThreadAbortException &&
41: HttpContext.Current != null &&
42: HttpContext.Current.Response.IsRequestBeingRedirected;
43: }
44: }
five star garden kim giang
ReplyDeletehọc kế toán tại hải phòng
học kế toán tại bắc giang
trung tâm kế toán tại thanh xuân
trung tâm kế toán tại bắc ninh
dich vu bao cao tai chinh
năng lượng linh hồn khác để cho mình hấp thu, sau đó lại thông qua chân
khí quỷ dị của mình truyền cho nàng một phần năng lượng linh hồn thì tốt
quá rồi.
Nghĩ vậy, Đoạn Vân rất tà ác mò mẫm Không gian giới tử trong tay mình.
Đúng a, trong giới tử không phải có hơn một trăm Điểu nhân sao? Điểu
nhân không đúng là linh hồn ngâm qua Chuyển Sinh Trì mới tạo thành sinh
vật đặc thù hay sao? Linh hồn hẳn là rất cường đại a! Chỉ cần giết chết
một Điểu nhân đã bị đóng băng lại, sau đó hút năng lượng linh hồn của
hắn, thuận tiện đem một chút năng lượng truyền cho linh hồn Hi Nhã, cái
này không phải có thể sao?
Vừa nói là làm!Sau đó Đoạn Vân từ trong Không gian giới tử lấy ra một
Điểu nhân bị đóng băng, sau đó làm tan băng, làm cho Điểu nhân từ chết
giả biến thành chết thật. Trong nháy mắt khi Điểu nhân chết, một linh
hồn cường đại từ trên người Thiên sứ đã chết liền xông ra. Cảm nhận được
sự bổ ích lớn của linh hồn đó, Đoạn Vân cũng không khách khí trực tiếp
vận chuyển chân khí hấp thu lại. Đoạn Vân ngưng tụ một cỗ tinh thần lực
cường đại, đưa linh hồn Thiên sứ bao vây lại, vận chuyển chân khí đưa
năng lượng nọ chuyển hóa thành chân khí và tinh thần lực rồi Đoạn Vân
truyền cho linh hồn Hi Nhã.
Quả nhiên, dưới sự gào thét của Thiên sứ, năng lượng linh hồn Thiên sứ
nhanh chóng bị Đoạn Vân hấp thu, chân khí và tinh thần lực của Đoạn Vân
vào lúc này cũng có thể thấy được tốc độ gia tăng. Đương nhiên có một cỗ
năng lượng không nhỏ vẫn y nguyên từ trong cơ thể Đoạn Vân truyền tới
kế toán cho giám đốc
trung tâm kế toán tại quảng ninh
học kế toán tại bắc ninh
học kế toán tại hà đông
trung tâm dạy kế toán tại tphcm
trung tâm đào tạo kế toán tại quảng ninh
học kế toán tại thanh xuân
khoá học kế toán thuế
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
ReplyDeleteI feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
ReplyDeletejava training in bangalore
java training in bangalore
I Got Job in my dream company with decent 12 Lacks Per Annum salary, I have learned this world most demanding course out there in the current IT Market from the python training in btm experts who helped me a lot to achieve my dreams comes true. Really worth trying
ReplyDeleteI Got Job in my dream company with decent 12 Lacks Per Annum salary, I have learned this world most demanding course out there in the current IT Market from the Hkbk group of institutions experts who helped me a lot to achieve my dreams comes true. Really worth trying
ReplyDeleteHi,
ReplyDeleteGood job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Data Science Training in btm. Because data science course in Bangalore is one of the best that one can do while choosing the course.
Good,keep posting...
ReplyDeletefreeinplanttrainingcourseforECEstudents
internship-in-chennai-for-bsc
inplant-training-for-automobile-engineering-students
freeinplanttrainingfor-ECEstudents-in-chennai
internship-for-cse-students-in-bsnl
application-for-industrial-training
Good
ReplyDeleteinterview-questions/aptitude/permutation-and-combination/how-many-groups-of-6-persons-can-be-formed
tutorials/oracle/oracle-delete
technology/chrome-flags-complete-guide-enhance-browsing-experience/
interview-questions/aptitude/time-and-work/a-alone-can-do-1-4-of-the-work-in-2-days
interview-questions/programming/recursion-and-iteration/integer-a-40-b-35-c-20-d-10-comment-about-the-output-of-the-following-two-statements
Thanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for any Big Data related information, please visit our website Big Data training institute in Bangalore.
ReplyDelete