I would write your code in this way:
Exception ex = Server.GetLastError();
bool? hasError = ex?.InnerException?.Message?.Contains("Network Transport");
if (hasError.HasValue && hasError.Value)
{
Response.Redirect("~/TestError.aspx");
}
You cannot convert a boolean to a string and then check for null. Just use the boolean.
Actually, even if we take for granted that ex is not null, InnerException is not null and Message is not null, Contains returns a boolean and converting it to a string gives "true" or "false" so the if condition is never true.
So you should really add the Null Conditional Operator to defend yourself by a Null Reference Exception if any of the inner objects is null and use the resulting nullable boolean to check if you need to redirect or not
In case your current C# language version doesn't support the Null Conditional Operator (confusing called also Null Propagation Operator) you need to apply a nested if logic
if (ex != null &&
ex.InnerException != null &&
ex.InnerException.Message != null)
if(ex.InnerException.Message.Contains("Network Transport"))
Response.Redirect("~/TestError.aspx");
but you should really upgrade the language version instead...