In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to efficiently use Response.Redirect to solve some unnecessary problems". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Response.Redirect efficiently to solve some unnecessary problems".
Description:
For example, if you have a web form, you need to validate some conditions and redirect the user jump if the conditions do not match.
The copy code is 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 affects scalable performance. Because it will terminate the thread pool. Now, just replace Response.Redirect ("Unauthorized.aspx") with Response.Redirect ("Unauthorized.aspx", false). This will solve the problem of thread termination, but will not stop the current page life cycle. That is, you need to make sure that the btnSave_Click event (and all other page times) because anyone can easily send a POST request as long as the btnSave_Click event is allowed to execute. To solve this problem, I recommend using the RedirectUser extension method.
The copy code is 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 benefit of using RedirectUser is that it will first use the Response.Redirect (with endResponse= false) method that is well extensible for the application. . The second benefit is that it does not overwrite the previous Response.Redirect (if any) after you call this method multiple times. The third benefit is that it calls HttpApplication.CompleteRequest to handle all events passed by the ASP.NET runtime and to filter HTTP pipeline information (not page lifecycle pipeline information). In addition, you need to pay attention to checking Response.IsRequestBeingRedirected in the btnSave_Click event. I also want you to put all your internal controls on the Response.IsRequestBeingRedirected check.
The copy code is as follows:
Another thing you need to pay attention to when you use a complex control (such as GridView, RadGrid, etc) with select, insert, update and delete events. When Response.IsRequestBeingRedirected is true, you must cancel these events (insert, update, or delete). Here is an example
The copy code is as follows:
Protected void GridView1_RowEditing (object sender, GridViewEditEventArgs e)
{
If (Response.IsRequestBeingRedirected)
{
E.Cancel = true
Return
}
}
Thank you for your reading, the above is the content of "how to efficiently use Response.Redirect to solve some unnecessary problems". After the study of this article, I believe you have a deeper understanding of how to efficiently use Response.Redirect to solve some unnecessary problems, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.