| By Andreas Grabner | Article Rating: |
|
| June 30, 2010 04:39 PM EDT | Reads: |
5,103 |
Last week I got the following two questions from one of our clients
- “We use ASP.NET PostBacks but can’t find the PurePath for the request triggering the PostBack handler – any hints?”
- “We see many ThreadAbortExceptions in our ASP.NET Application and we are not sure why they happen – are they expected?”
Time for a little blog that gives some internals on PostBacks as well as Redirects (which are commonly used in PostBack handlers).
What is a PostBack and how does it work?
It’s not my intention to write the 50th blog post about PostBacks – I’d rather recommend checking out the following links for more details on ASP.NET PostBacks and the ASP.NET Page LifeCycle. I am also not necessarily advocating PostBacks here either as there are other ways of implementing web applications (e.g.: ASP.NET MVC) – but – as the question came up I am sure many out there use this feature and may wonder how it works.
The important thing to understand is that a PostBack – as the name implies – POSTs data back to the current url. This allows ASP.NET Developers to implement PostBack code similar to event handlers in rich UI’s where you even get access to before/after values of controls.
In the sample application I use I have a MainForm.aspx page that displays a login control with a username and password field and a login button. When the user clicks the login button the data of the form elements is POSTed back to the server which ultimately calls the Login Button Click event handler. When a user therefore opens a browser and browses to my MainForm.aspx page and then clicks the login button we end up having 2 HTTP Requests to the same MainForm.aspx page. The second request responds with a different page, e.g.: Yes you are logged in or Authentication failed even though it goes to the same URL.
The following illustration shows my login button handler that takes the values POSTed to the MainForm.aspx page and checks whether the username/password combination is correct.
PostBack triggers Login Button Event Handler which does user authentication and redirects depending on logon success
Also – depending on whether it is the initial page request or a PostBack you may want to execute different code. This can be done by checking the IsPostBack property on the Page objects. Remember our earlier example? The user basically requested the same url – MainForm.aspx – but obviously expects a different result for the 2nd request. The following illustration shows how to implement conditional code depending on PostBack or initial request:
The answer to the first question I received last week therefore is: Identify the 2 requests on the same initial URL. The following illustration shows how dynaTrace displays the individual URL’s that have been traced so far and how to actually drill to the PurePath for the PostBack request. We see the 2 requests to MainForm.aspx and the two corresponding PurePaths where the second is the one that reflects the PostBack request which ultimately calls our click handler:
What’s the difference between Response.Redirect and Server.Transfer?
You probably wonder what really happens after a PostBack. Are we now always staying on the same MainForm.aspx page? Or is there a way to redirect the user to a different url? ASP.NET offers two options (at least I am only aware of these two). There is the option of redirecting the user to a different URL or let the server transfer over to process a different URL.
Let’s go back to my example from above. My user logs in. In case the login is successful I want the user to get to his personalized site. In case the login was not successful I want the user to get back to the login page with an error displayed that the login was not successful. One option to redirect the user to a different url is to use Response.Redirect telling ASP.NET to redirect to a different URL. Technically – Response.Redirect actually sends an HTTP Redirect Response back to the browser causing the browser to request the new URL. This works great – but – it causes an extra roundtrip between browser and server. The following illustration (using the FREE dynaTrace AJAX Edition) shows the Network Requests between browser and server when browsing through the pages of my application. We can spot the redirects that the PostBacks cause by using Response.Redirect:
My sample application redirects to the same URL but using a different url parameter to indicate which feature of the web site my user should see, e.g.: overview. It is easy to spot what is going on here. First we have the initial request to MainForm.aspx which displays the login dialog (because my user is not yet logged in). The PostBack on MainForm.aspx comes back telling the browser to redirect to MainForm.aspx?action?overview which is then requested by the browser. This behavior goes on for every interaction my user does – basically causing an extra roundtrip that should be avoided. It also causes many exceptions on the server – and that brings me to the second question I received: In order to do the redirect ASP.NET aborts the currently executing thread with a ThreadAbortException. So – whenever you use Response.Redirect – expect to see ThreadAbortExceptions (in case you use something like dynaTrace that actually gives you insight into these internal exceptions).
Fortunately ASP.NET offers a second option to transfer to a different page. Server.Transfer shortcuts the redirect by executing another page request with the new URL in your current ASP.NET thread. When changing my implementation from Response.Redirect to Server.Transfer I get rid of all HTTP Redirects. The following illustration shows what a PurePath that uses Server.Transfer looks like:
Conclusion
Make yourself familiar with internals of ASP.NET. If you use other frameworks such as ASP.NET MVC make sure you do enough research and reading on the options the framework gives you and how things work internally. It is important to save on unnecessary HTTP roundtrips as they can become really expensive for end-user browsing experience. As always, feedback and your thoughts on this topic are welcome.
Related reading:
- SharePoint: Performance Problem with LookupField Control The Microsoft.SharePoint.WebControls.LookupField control is used as the default control when editing...
- SharePoint: List Performance – How list column indices really work under the hood Have you ever wondered what is really going on under...
Published June 30, 2010 Reads 5,103
Copyright © 2010 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Andreas Grabner
Andreas has over a decade of experience as an architect and developer, and currently works as a senior performance architect and technology strategist for dynaTrace Software, where he influences product strategy and works closely with customers in implementing performance management solutions across the application life cycle. He is a regular speaker at software conferences, writes for a number of technology publications, and blogs at http://blog.dynatrace.com
- Cloud People: A Who's Who of Cloud Computing
- Windows Azure IaaS Reaches General Availability
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- CollabNet And UC4 Announce General Availability Of Joint Enterprise DevOps Platform
- Symphony EYC Appoints New Account Manager to Drive Global Opportunities
- Cloud Computing Is Simplifying Things
- Cloud Expo New York: Developing the World’s First IaaS Marketplace
- Session Topics: 12th Cloud Expo / Cloud Expo New York
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Best CIO Practices Shared from SHI’s Customers
- Windows Azure IaaS Reaches General Availability
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- The PostOpen Event – Why It Is So Important
- The Cover and the Epilogue of the Upcoming Book
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Small Cancers, Big Data, and a Life Examined
- Global Micro Servers Market (2013 - 2018), By Processor Type (Intel, Arm, Amd), Component (Hardware, Software, Operating System), Application (Media Storage, Data Centers, Analytics, Cloud Computing) & Geography (North America, Europe, Apac, Row)
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Where Are RIA Technologies Headed in 2008?
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"































