This post is about something which is widely found on the net, you will find many hits with google on the (at first sight) unlogical combination of Smartnavigation and Redirect. Nevertheless it kept me busy for some time. There seemed to be no relation between the two things untill I found out what was going on. Maybe this will save some of you a little time.
In an application I have implemented a general error handling routine. It collects some information and redirects the application to an error page which sums up all. Everything worked like a snap up till last version, which only produced blank screens. In this last version I had also set the smartnavigation property of the pages and that was exactly what messed up my error handling. Smartnavigation takes care of a couple of things
- Prevent all postbacks of the same page building up in the navigation history of the browser
- Eliminate "navigation flash"
- Persist scroll position over postbacks
- Persist focus over postbacks
The last two points are not always what you want. Consider changing the selected item in a datagrid from an item in the top of the list to one in the bottom. More on that in an upcoming article. But the first point is reason enough to switch smartnavigation on. What the docs don't tell you (but what you could have guessed) is that smartnav fiddles with the context of your response. The Redirect method of the response no longer works, breaking my error handler. The docs advise you In most circumstances, do not set this property in code, but that was exactly the way to repair my errorhandler. In the snippet p is the page on which the error occured, the code will redirect the app to newURL, a string which contains the URL of the error displaying page. The info is in the querystring.
p.SmartNavigation =
false;
context.Response.Redirect(newURL, true);
Smartnavigation does not seem to have an influence on the request. A member of the request is an URLreferrer property, it is the URL your app last visited before arriving on this page. The first time the page is requested it is the page you came from but after a postback it is always the same as the URL of the page itself. With and without smartnavigation. Considering the first of the reasons to use smartnav it might seem logical that the property would keep pointing at the last different page visited. But it's hard to mimic state.
Blog on,
Peter