Error : [System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

Target: I was working on custom Login page to redirect user on specific site based on their user type (e.g Admin User, Manager User etc) using Claims Based Authentication using response.redirect in try catch block in c# and i was getting following error

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

also my thread was being Aborted but i was able to redirect on specific site.
So the error was very weird. I analyze the code again and found solution 🙂

Solution:
I did some try with code and found when i was commenting ‘try’-‘catch’ block it was working fine. But its quite mandatory for me to use ‘try’-‘catch’ block.
So i found another work around to get user redirected successfully on specific site.

In Response.Redirect, i passed second parameter as “False”, the reason behind this is – the thread of page from which i was redirecting to a new page/site was not executed completely and i was redirecting it to a new page/site so the error was coming.

Below is a sample code snippet which i used.

https://mysharepointsolution.files.wordpress.com/2012/04/login-error.jpg

Hope this will help someone…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s