惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
T
Threatpost
T
Tenable Blog
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
C
Cisco Blogs
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
V
V2EX
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
PCI Perspectives
PCI Perspectives
P
Privacy International News Feed
D
Docker

博客园 - Ranran

ASP.NET 5与MVC 6中的新特性 .NET技术大系概览 (迄今为止最全的.NET技术栈) 7 天玩转 ASP.NET MVC — 第 1 天 腾讯web前端笔试题及个人答案 11个Visual Studio代码性能分析工具 CLR 这些年有啥变化吗? ASP.NET 大文件下载的实现思路及代码 7个Linux和Ubuntu下的免费CSS编辑器 11个很棒的 jQuery 图表库 javascript客户端检测技术 编写更好的C#代码 ASP.NET中gridview获取当前行的索引值 .NET逻辑分层架构总结 ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件 ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式) ASP.NET中利用DataList实现图片无缝滚动 老码农教你在 StackOverflow 上谈笑风生 ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式) Asp.net面试题
高效的使用 Response.Redirect
Ranran · 2015-07-21 · via 博客园 - Ranran

介绍:

  我正在评估一个 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)

        {

        }

  这样做很好,但这会影响可扩展性能。因为它将会终止线程池.现在,只需要用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:

            }

        }

        protected void btnSave_Click(object sender, EventArgs e)

        {

            if (Response.IsRequestBeingRedirected)

            {

                return;

            }

        }

    }

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

  另一件你需要注意的事情,当你使用一个复杂的控制(类似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