Reputation: 101
I'm writing a class to validate sessions.
I get the this error
Thread was being aborted
and on researching found that the way around it is to use the false
parameter, but using that parameter does not redirect and instead allows the code after the redirect line to execute.
There must be something simple and fundamental here that I'm missing, below is my code.
It breaks in the InvalidAccess()
method.
Public Sub New()
CheckSessionCustomerID()
If GotConnectionString() = False Then
InvalidAccess()
End If
End Sub
Public Function GotConnectionString() As Boolean
GotConnectionString = False
Try
If PublicDBConnectionStringName.Trim = String.Empty Then
GotConnectionString = False
Else
PublicConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(PublicDBConnectionStringName).ToString
If PublicConnectionString.Trim <> String.Empty Then
GotConnectionString = True
End If
End If
Catch ex As Exception
ErrorLogging.LogError(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name.ToString(), PublicBBTCustomerID & "|" & ex.Message.ToString())
GotConnectionString = False
End Try
End Function
Public Sub CheckSessionCustomerID()
Dim SessionEmployeeID As Integer
Try
If PublicTesting Then
SessionEmployeeID = 1
Else
If IsNothing(HttpContext.Current.Session("EmployeeIDLoggedIn")) Then
InvalidAccess()
ElseIf HttpContext.Current.Session("EmployeeIDLoggedIn").ToString = "0" Then
InvalidAccess()
Else
SessionEmployeeID = Val(HttpContext.Current.Session("EmployeeIDLoggedIn"))
HttpContext.Current.Session("EmployeeIDLoggedIn") = SessionEmployeeID.ToString()
End If
End If
Catch ex As Exception
InvalidAccess()
End Try
End Sub
Private Sub InvalidAccess()
Try
System.Web.HttpContext.Current.Response.Redirect("/InvalidAccess.aspx")
Catch ex As Exception
System.Web.HttpContext.Current.Response.Redirect("/Login.aspx?ID=" & PubliCustomerID & "&ID2=5")
End Try
End Sub
Upvotes: 0
Views: 3945
Reputation: 4288
You can use the false parameter on the Response.Redirect but then also you have to complete the request to stop execution if that's what you want:
Response.Redirect("/InvalidAccess.aspx", False)
HttpContext.Current.ApplicationInstance.CompleteRequest()
http://www.blakepell.com/asp-net-avoid-threadabortexception-on-response-redirect
Upvotes: 0
Reputation: 5144
The Thread was being aborted is completely normal when you redirect and wish to stop further execution of request.
In your InvalidAccess
method remove Try...Catch
block - it is meaningless.
The Response.Redirect
will never throw an exception you are interested in.
It is not very clear what have you tried to achieve with this try...catch
in your method.
Alternatively, you can use false
parameter in Response.Redirect
.
Upvotes: 1