Efficient use of Response.Redirect to solve some unnecessary problems _ practical skills

Source: Internet
Author: User
Tags static class
Introduced:

I'm evaluating a asp.net Web project application. It has some scalability issues. This means that when the Web site visits increase. The system will become slow. When I look at the application log. I found a lot of threadabortexception. This application uses a lot of Response.Redirect (yes endresponse= true), which is the root cause of scalability problems. This problem will be resolved by Endresponse = False in Response.Redirect. But doing so can cause some strange problems in the application. Because the application will assume that the Response.Redirect will stop executing on the current page. Besides, you need to deal with some security risks, because your application assumes that page events will never be redirected. In this article, I'll tell you a simple way to solve these problems and get good performance

Description

For example, if you have a Web form, you need to validate some conditions and redirect the user to jump when the condition does not match.
Copy Code code as follows:

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 good, but it can affect scalable performance. Because it will terminate the thread pool. Now, just replace the Response.Redirect ("unauthorized.aspx") with Response.Redirect ("unauthorized.aspx", false). This resolves the problem of thread termination, but does not stop the current page life cycle. That is, you have to make sure that the Btnsave_click event (and all other page times) is easy to send a POST request as long as the Btnsave_click event is allowed to be executed by anyone. To solve this problem I recommend using the Redirectuser extension method.
Copy Code code as follows:

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 advantage of using Redirectuser is that it will first use the Response.Redirect (with endresponse= false) method that has a good extensibility for the application ... The second advantage is that it does not overwrite the previous response.redirect (if any) after you call this method multiple times. A third advantage is that it invokes httpapplication.completerequest to process the ASP. NET Runtime and filter HTTP pipe information (not page lifecycle pipeline information). Plus, you need to be aware of checking in the Btnsave_click event. Response.isrequestbeingredirected. I also want you to put all your internal controls on the response.isrequestbeingredirected check,
Copy Code code as follows:

<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 pay attention to when you use a complex control (like GridView, RadGrid, etc) with these options, inserts, updates, and deletes events. When response.isrequestbeingredirected is true, you must cancel the operation (insert, update, or delete) These events, the following is an example
Copy Code code as follows:

protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)
{
if (response.isrequestbeingredirected)
{
E.cancel = true;
Return
}
}

Summarize:

In this article, I show you how to use Response.Redirect. I have also identified a number of risk issues. Response.Redirect optimization and technology can be used to reduce risk. Also hope you like this article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.