介绍:

  我正在评估一个 ASP.NET Web 项目应用。它有一些可扩展性问题。意味着当网站访问量增加的时候。系统将会变得缓慢。当我查看应用日志。我找到了大量的 ThreadAbortException. 这个应用大量的使用了 Response.Redirect (是的 endResponse= true),这个就是可扩展性问题的根源。通过endResponse = false 在Response.Redirect将会解决这个问题. 但这样做会导致应用程序出现一些奇怪的问题。因为应用程序将假设在 Response.Redirect 将在当前页面停止执行.除此之外你需要处理一些安全隐患,因为你的应用程序是假设页面事件永远不会执行重定向之后。在这篇文章中,我将讲述一个简单的方法来解决这些问题,并取得良好性能

  说明:

  比方说你有一个web表单,需要验证一些条件并在条件不符时重定向用户跳转。

1
2
3
4
5
6
7
8
9
10
11
12
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
        }

  这样做很好,但这会影响可扩展性能。因为它将会终止线程池.现在,只需要用Response.Redirect("Unauthorized.aspx", false)替换Response.Redirect("Unauthorized.aspx") . 这将解决线程终止的问题,但不会停止当前页面生命周期. 也就是说,你有需要确保 btnSave_Click 事件(和所有其他页面时间)因为只要允许btnSave_Click事件执行任何人都可以很容易地发送POST请求. 为了解决这个问题我推荐使用RedirectUser扩展方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.<span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=asp&k0=asp&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">asp</span></a></span>x");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Response.IsRequestBeingRedirected)
            {
                return;
            }
            // Save Data Here
        }
    }

  使用 RedirectUser 第一个好处是它将首先使用对于应用程序具有良好扩展性的Response.Redirect(with endResponse= false) 方法。.第二个好处就是在你多次调用这个方法后它不会覆盖先前的Response.Redirect(如果有的话). 第三个好处是它会调用 HttpApplication.CompleteRequest用来处理  ASP.NET运行时所有通过的事件以及过滤 HTTP 管道信息(不是页面生命周期管道信息).另外你需要注意在 btnSave_Click事件中检查 Response.IsRequestBeingRedirected.我也希望你把所有的内部控制放到 Response.IsRequestBeingRedirected 检查,

1
2
3
4
5
6
7
8
<form id="form1" runat="<span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=server&k0=server&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">server</span></a></span>">
        <% if(!Response.IsRequestBeingRedirected){ %>
            <<span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=asp&k0=asp&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">asp</span></a></span>:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            <%--All the Other Controls--%>
            <%--All the Other Controls--%>
            <%--All the Other Controls--%>
        <%} %>
    </form>

  另一件你需要注意的事情,当你使用一个复杂的控制(类似GridView, RadGrid, etc)这些拥有 选择,插入,更新和删除事件时。 当 Response.IsRequestBeingRedirected 为true时,你必须取消操作(插入,更新或删除) 这些事件,下面是一个例子

1
2
3
4
5
6
7
8
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            if (Response.IsRequestBeingRedirected)
            {
                e.Cancel = true;
                return;
            }
        }

总结:

  在这篇文章里,我向您展示如何使用Response.Redirect . 我同样也发现了一些风险问题。可以采用Response.Redirect优化和技术以降低风险 .也同样希望你喜欢这篇文章。

  原文地址:using-response-redirect-effectively.aspx

高效的使用 Response.Redirect的更多相关文章

  1. 高效的使用Response.Redirect解决一些不必要的问题(转载)

    这篇文章主要介绍了如何高效的使用 Response.Redirect解决一些不必要的问题,需要的朋友可以参考下 介绍: 我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当 ...

  2. 开发技巧:高效的使用 Response.Redirect

    我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortException. 这 ...

  3. Response.Redirect与Server.Transfer区别-转

    执行过程: 1.浏览器ASP文件请求->服务器执行->遇到response.redirect语句->服务器发送response.redirect后面的地址给客户机端的浏览器-> ...

  4. Response.Redirect引起的性能问题分析

    现象: 最近做的一个系统通过单点登录(SSO) 技术验证用户登录.用户在SSO 系统上通过验证后,跳转到该系统的不同模块.而跳转的时间一直维持子啊几分钟左右. 分析步骤: 在问题复现时抓取Hang d ...

  5. 一个由Response.Redirect 引起的性能问题的分析

    现象: 某系统通过单点登录(SSO) 技术验证用户登录.用户在SSO 系统上通过验证后,跳转到某系统的主页上面.而跳转的时间很长,约1分钟以上. 分析步骤: 在问题复现时抓取Hang dump 进行分 ...

  6. 关于Response.Redirect 端口不一致的跳转

    如果内网和外网的端口号设置的不相同,那在使用Response.Redirect跳转的时候会无法成功.需要做以下设置: <system.web> <httpRuntime useFul ...

  7. Server.Transfer 和 Response.Redirect 用法区别

    在ASP.NET中,在后台传值方式目前大多都是用 Response.Redirect("页面地址") 来重定向页面的,但是现在还有一种方式也可以达到重定向页面的作用,而且在某些时刻 ...

  8. 让Response.Redirect页面重定向更有效率

    用 Redirect 方法可将浏览器重定向到另一个 URL,而不是将内容发送给用户. 这里有一篇文章介绍使用Redirect<Using Response.Redirect Effectivel ...

  9. try catch中用了 Response.Redirect 引发的线程异常终止

    记录一下,提醒自己. 今天写代码的时候,在try 中写了一句  Response.Redirect 在 catch 把页面重定向到了另外一个地方 本来是想打算,如果没出现异常,就定到页面A,如果异常了 ...

随机推荐

  1. CSS文件开头到底声明@charset "utf-8"

    @charset "utf-8" 这个声明是告诉浏览器[我的CSS文件是UTF-8编码的],但实际上这个文件可以不用是UTF-8编码的,所以这个声明能达到欺骗浏览器的作用. 但实践 ...

  2. 【Alpha版本】冲刺-Day3

    队伍:606notconnected 会议时间:11月11日 会议总结 张斯巍(433) 今天安排:个人信息界面设计 完成度:100% 明天计划:个人信息界面设计 遇到的问题:ps掌握的还不够熟练,导 ...

  3. POJ2528Mayor's posters(离散化 + 线段树)

    题目链接: 题意:给定一些高度都相同的海报去贴,问最后能看见几张海报 The picture below illustrates the case of the sample input. { 8,9 ...

  4. JS-鼠标经过显示二级菜单

    在css处添加了border样式为了看得更清楚——源代码有一个程序漏洞,存在一个很烦人的大bug. <ul class="nav"> <li class=&quo ...

  5. IOS 中的CoreImage框架(framework)

    http://www.cnblogs.com/try2do-neo/p/3601546.html coreimage framework 组成 apple 已经帮我们把image的处理分类好,来看看它 ...

  6. DataReader和DataSet的异同

    DataReader:使用时始终占用SqlConnection,在线操作数据库:每次只在内存中加载一条数据,所以占用的内存是很小的:是只进的. 只读的. DataSet:则是将数据一次性加载在内存中. ...

  7. bootstrap panel 和table的使用

    一.HTML中的页面内容 <div class="col-sm-12"> <!-- <div class="m-b-md" style= ...

  8. IT项目管理感悟

    做项目的时候,切记做好需求分析,列出需求清单,需要考虑好每一个细节,一个机柜是否需要PDU,KVM,服务器放置位置,放置几台服务器,服务器是几U的,IP段规划都需要事先规划好,并且文档化.---记住这 ...

  9. Nginx环境下http和https可同时访问方法

    给nginx配置SSL证书之后,https可以正常访问,http访问显示400错误,nginx的配置如下: server { listen 80 default backlog=2048; liste ...

  10. GLSL Debugger的姿势

    https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/OpenGLShaderBuilder ...