对于想了解Response.End()被认为是有害的吗?的读者,本文将是一篇不可错过的文章,我们将详细介绍responses.,并且为您提供关于asp.net–Response.End()和Respo
对于想了解Response.End() 被认为是有害的吗?的读者,本文将是一篇不可错过的文章,我们将详细介绍responses.,并且为您提供关于asp.net – Response.End()和Response.Flush()之间的差异、asp.net – 在Response.End之前使用Response.Flush、asp.net – 在Response.Redirect()之后调用Response.End()、c – 这些被认为是魔术数字吗?的有价值信息。
本文目录一览:- Response.End() 被认为是有害的吗?(responses.)
- asp.net – Response.End()和Response.Flush()之间的差异
- asp.net – 在Response.End之前使用Response.Flush
- asp.net – 在Response.Redirect()之后调用Response.End()
- c – 这些被认为是魔术数字吗?
Response.End() 被认为是有害的吗?(responses.)
这篇知识库文章说
ASP.NETResponse.End()
中止了一个线程。
反射器显示它看起来像这样:
public void End(){ if (this._context.IsInCancellablePeriod) { InternalSecurityPermissions.ControlThread.Assert(); Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false)); } else if (!this._flushing) { this.Flush(); this._ended = true; if (this._context.ApplicationInstance != null) { this._context.ApplicationInstance.CompleteRequest(); } }}
这对我来说似乎很苛刻。正如知识库文章所说,应用程序中的任何代码Response.End()
都不会被执行,这违反了最小惊讶原则。这几乎就像Application.Exit()
在
WinForms
应用程序中一样。导致的线程中止异常Response.End()
是不可捕获的,因此将代码包围在try
…finally
中不会满足。
这让我想知道我是否应该总是避免Response.End()
。
任何人都可以建议,我应该何时使用Response.End()
,何时Response.Close()
何地HttpContext.Current.ApplicationInstance.CompleteRequest()
?
参考:Rick Strahl 的博客条目。
根据我收到的意见,我的回答是, 是的,Response.End
是有害的,但它在某些有限的情况下很有用。
- 用作
Response.End()
不可捕获的抛出,HttpResponse
在异常情况下立即终止。在调试期间也很有用。 避免Response.End()
完成例行的反应。 - 用于
Response.Close()
立即关闭与客户端的连接。根据此 MSDN 博客文章,此方法 不适用于正常的 HTTP 请求处理。 您极不可能有充分的理由调用此方法。 - 用于
CompleteRequest()
结束正常请求。 导致 ASP.NET 管道在当前事件完成后CompleteRequest
跳转到该事件。因此,如果您调用,然后在响应中写入更多内容,则写入内容将发送到客户端。EndRequest``HttpApplication``CompleteRequest
编辑 - 2011 年 4 月 13 日
此处提供了进一步的说明:
- MSDN 博客上的有用帖子
- Jon Reid 的有用分析
答案1
小编典典ThreadAbortException
如果您在应用程序上使用了异常记录器,它会被这些良性Response.End()
调用中的 s
淡化。我认为这是微软说“Knock it off!”的方式。
我只会Response.End()
在有一些特殊情况并且不可能采取其他行动的情况下使用。也许那时,记录此异常实际上可能表示警告。
asp.net – Response.End()和Response.Flush()之间的差异
context.HttpContext.Response.Clear(); context.HttpContext.Response.Write(htmlString); context.HttpContext.Response.End();
但是,当页面加载时,我已经没有关闭html标签.当我用Response.Flush()替换Response.End()它工作正常.
Response.End()和Response.Flush()之间有什么区别?
解决方法
Forces all currently buffered output to be sent to the client. The
Flush method can be called multiple times during request processing.
到Response.End
Sends all currently buffered output to the client,stops execution of
the page,and raises the EndRequest event.
如果在Response.Write之后没有对页面进行任何处理,并且希望停止处理页面,则应该尝试使用此代码.
context.HttpContext.Response.Clear(); context.HttpContext.Response.Write(htmlString); context.HttpContext.Response.Flush(); // send all buffered output to client context.HttpContext.Response.End(); // response.end would work fine Now.
asp.net – 在Response.End之前使用Response.Flush
解决方法
asp.net – 在Response.Redirect()之后调用Response.End()
更新
感谢所有的答案.因为有些答案说这是必要的而有些人说没有,我已经搜索了更多,并在msdn的评论中发现了以下内容:
重定向调用End,在完成时引发ThreadAbortException异常.
解决方法
我不同意它的良好做法 – 它会导致误导性的代码.
MSDN:
Redirect calls End which raises a ThreadAbortException exception upon completion.
HTTP的定律解释了一旦响应被发送,服务器就完成了(不再调用代码)
c – 这些被认为是魔术数字吗?
在下面的函数中,我的数组索引器是否会被视为幻数?
码:
string CalcGrade(int s1,int s2,int s3,double median) { const int SIZE = 23; const int LETTER_GRADE_BARRIERS[SIZE] = { 400,381,380,361,360,341,340,321,320,301,300,281,280,261,260,241,240,221,220,201,200,181,180 }; double finalGrade; string letterGrade; finalGrade = s1 + s2 + s3 + median; if (finalGrade >= LETTER_GRADE_BARRIERS[1] && finalGrade <= LETTER_GRADE_BARRIERS[0]) { letterGrade = "A"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[3] && finalGrade <= LETTER_GRADE_BARRIERS[2]) { letterGrade = "A-"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[5] && finalGrade <= LETTER_GRADE_BARRIERS[4]) { letterGrade = "B+"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[7] && finalGrade <= LETTER_GRADE_BARRIERS[6]) { letterGrade = "B"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[9] && finalGrade <= LETTER_GRADE_BARRIERS[8]) { letterGrade = "B-"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[11] && finalGrade <= LETTER_GRADE_BARRIERS[10]) { letterGrade = "C+"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[13] && finalGrade <= LETTER_GRADE_BARRIERS[12]) { letterGrade = "C"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[15] && finalGrade <= LETTER_GRADE_BARRIERS[14]) { letterGrade = "C-"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[17] && finalGrade <= LETTER_GRADE_BARRIERS[16]) { letterGrade = "D+"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[19] && finalGrade <= LETTER_GRADE_BARRIERS[18]) { letterGrade = "D"; } else if (finalGrade >= LETTER_GRADE_BARRIERS[21] && finalGrade <= LETTER_GRADE_BARRIERS[20]) { letterGrade = "D-"; } else if (finalGrade <= LETTER_GRADE_BARRIERS[22]) { letterGrade = "Fail"; } return letterGrade; }
谢谢!
解决方法
除非你是一个真正的大师,否则你可能也被允许自由使用两个权力:-)
顺便说一句,你可能可以重构那些代码,使其更容易理解,例如:
string CalcGrade (int s1,double median) { // Grade lookup arrays. If grade is >= limit[n],string is grades[n]. // Anything below D- is a fail. static const int Limits[] = {400,180 }; static const int Grades[] = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","D-"}; double finalGrade = s1 + s2 + s3 + median; // Check each element of the array and,if the final grade is greater // than or equal to,return the grade string. for (int i = 0; i < sizeof(Limits) / sizeof(*Limits); i++) if (finalGrade >= Limits[i]) return Grades[i]; // Otherwise,Failed. return "Fail"; }
这将删除遍布代码的魔术数字到一个区域,在这个区域中,它们的工作方式很明显(假设您很好地对齐它们).
它还消除了原始解决方案的一个问题,即我们对得分为380.5的人做了什么 – 失败那些人并不公平:-)或者为400以上的人分配一个等级(因为那里没有)看起来似乎是回归“A”的一种方式.
关于Response.End() 被认为是有害的吗?和responses.的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net – Response.End()和Response.Flush()之间的差异、asp.net – 在Response.End之前使用Response.Flush、asp.net – 在Response.Redirect()之后调用Response.End()、c – 这些被认为是魔术数字吗?等相关知识的信息别忘了在本站进行查找喔。
本文标签: