Use Response.Redirect efficiently to solve unnecessary problems

  • 2020-12-10 00:40:19
  • OfStack

Introduction:

I am evaluating an ES2en. NET Web application. It has a number of scalability issues. That means when the number of visitors to the site increases. The system will slow down. When I look at the application log. I found a lot of ThreadAbortException. This application uses a lot of Response.Redirect (yes, endResponse= true) and this is the root of the scalability problem. endResponse = false in ES12en. Redirect will solve this problem. But doing so can cause some strange problems with your application. Because the application will assume that execution in Response.Redirect will stop on the current page. In addition to this you need to deal with a security hazard because your application is assuming that page events will never perform redirects. In this article, I'll show you a simple way to solve these problems and get good performance

Description:

Let's say you have an web form and you need to verify 1 condition and redirect the user to jump if the condition doesn't match.
 
protected void Page_Load(object sender, EventArgs e) 
{ 
var condition = ......; 
if (!condition) 
{ 
Response.Redirect("SomePage.aspx"); 
} 
} 
protected void btnSave_Click(object sender, EventArgs e) 
{ 
// Save Data Here 
} 

This is all well and good, but it affects scalability performance. Because it will terminate the thread pool. Now, just replace Response.Redirect (" Unauthorized.aspx ") with ES24en.Redirect (" ES26en.aspx ", false). This will resolve the issue of thread termination, but will not stop the current page life cycle; that is, you need to ensure that the btnSave_Click event (and all other page times) is executed because anyone can easily send POST requests as long as the btnSave_Click event is allowed to execute. To solve this problem, I recommend using the RedirectUser extension method.
 
public static class HttpResponseExtensions 
{ 
public static void RedirectUser(this HttpResponse response, string url) 
{ 
if (response.IsRequestBeingRedirected) 
return; 
response.Redirect(url, false); 
var context = HttpContext.Current; 
if (context != null) 
{ 
context.ApplicationInstance.CompleteRequest(); 
} 
} 
} 
public partial class WebForm : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
var condition = .....; 
if (!condition) 
{ 
Response.RedirectUser("Unauthorized.aspx"); 
} 
} 
protected void btnSave_Click(object sender, EventArgs e) 
{ 
if (Response.IsRequestBeingRedirected) 
{ 
return; 
} 
// Save Data Here 
} 
} 

The first benefit of using RedirectUser is that it starts with the Response.Redirect (with endResponse= false) method, which scales well with your application. . The second benefit is that this method does not override the previous Response.Redirect (if any) after you call it multiple times. The third benefit is that it calls HttpApplication.CompleteRequest to handle all passing events at the ES52en.NET runtime and to filter HTTP pipe information (not page life cycle pipe information). Also note that you need to check Response.IsRequestBeingRedirected in the btnSave_Click event. I also want you to put all your internal controls in Response.ES60en check,
 
<form id="form1" runat="server"> 
<% if(!Response.IsRequestBeingRedirected){ %> 
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> 
<%--All the Other Controls--%> 
<%--All the Other Controls--%> 
<%--All the Other Controls--%> 
<%} %> 
</form> 

Another thing you need to be aware of when you use a complex control (like GridView, RadGrid, etc) that has select, insert, update, and delete events. When ES67en.IsRequestBeingRedirected is true, you must cancel (insert, update, or delete) these events. Here is an example
 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
if (Response.IsRequestBeingRedirected) 
{ 
e.Cancel = true; 
return; 
} 
} 

Conclusion:

In this article, I'll show you how to use Response.Redirect. I also found a number of risk issues. Response. Redirect optimizations and techniques can be used to reduce risk. Also hope you enjoy this article.

Related articles: