I have tried the following typical approaches but couldn't able to do a redirect from an asynchronous ASP.NET method:
Response.Redirect("~/Login.aspx");
HttpContext.Current.Response.Redirect("~/Login.aspx");
I have also tried Server.Transfer
but couldn't get success due to the unavailability of the page controls inside a reference method(delegate).
I have already tried a static property which I filled in delegate response and continuously checking it on client side using ASP.NET SignalR to perform a redirect but as it a static property, it redirects all the user to the login page which I don't want to do it.
private void Response_Recieved(Message objMessage)
{
try
{
if (objMessage.OperationType == Operation.Data)
{
NotificationMessage objNotifications = new DataProcess().Deserialize_Messages(objMessage);
_jsonData = JsonConvert.SerializeObject(objNotifications);
}
else if (objMessage.OperationType == Operation.ServerAbnormalDisconnect)
{
// I want to redirect a user to login page whenever server disconnects
HttpContext.Current.Response.Redirect("~/Login.aspx");
//Response.Redirect("~/Login.aspx");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
What would be the alternative or best approach to perform a redirect from a delegate function when there is no control available also without using any static property?
Here is what I had done years back. I am posting it here to help others who are asking me.
Well it is clearly can't be done from an asynchronous ASP.NET method(delegate).
So to achieve the desire functionality I passed a value to the client side from the method using normal broadcasting in SignalR.
And on client side, I am validating and performing actions accordingly. I have changed the URL using simple JavaScript. I am putting a simplest code to understand the core concept of redirection from ASP.NET using SignalR.
Code Behind