Development skills: Efficient Use of Response. Redirect

Source: Internet
Author: User

I am evaluating an ASP. NET Web project application. It has some scalability problems. This means that when the website traffic increases. The system will become slow. When I view the application log. I found a large number of ThreadAbortException. This application uses Response. Redirect in large quantities, which is the root cause of the scalability problem. Using endResponse = false in Response. Redirect will solve this problem, but this will cause some strange problems in the application. Because the application will assume that the execution will stop on the current page in Response. Redirect. In addition, you need to handle some security risks, because your application assumes that page events will never be redirected. In this article, I will describe a simple method to solve these problems and achieve good performance.

Note:

For example, if you have a Web form, you need to verify some conditions and redirect the user redirect when the conditions do not match.

123456789101112 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 will affect scalability. Because it will terminate the thread pool. now, you only need to use Response. redirect ("Unauthorized. aspx ", false) replace Response. redirect ("Unauthorized. aspx "). this will solve the thread termination problem, but will not stop the current page lifecycle. that is to say, you need to make sure that the btnSave_Click event (and all other page time) can easily send POST requests as long as the btnSave_Click event is allowed to be executed by anyone. to solve this problem, we recommend using the RedirectUser extension method.

123456789101112131415161718192021222324252627282930313233 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 with good scalability for the application .. The second advantage is that after you call this method multiple times, it will not overwrite the previous Response. redirect (if any ). the third advantage is that it calls HttpApplication. completeRequest is used to process ASP. all events passed during the NET runtime and HTTP MPs queue information (not page lifecycle MPs queue information ). in addition, check Response in the btnSave_Click event. isRequestBeingRedirected. I also want you to put all internal controls in Response. isRequestBeingRedirected check,

12345678 <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 is when you use a complex control (like GridView, RadGrid, etc) that has options, inserts, updates, and deletes events. When Response. IsRequestBeingRedirected is true, you must cancel the operations (insert, update, or delete). The following is an example.

12345678 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)        {            if (Response.IsRequestBeingRedirected)            {                e.Cancel = true;                return;            }        }

Summary:

In this article, I will show you how to use Response. Redirect. I also found some risk issues. You can use Response. Redirect optimization and technology to reduce risks. I also hope you like this article.

Address: using-response-redirect-effectively.aspx

Link: http://www.cnblogs.com/oooweb/p/using-response-redirect-effectively.html

Related 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.