Monday, August 16, 2010

Issues using Response.Redirect in Silverlight

I was working with one of customer's today, on a Silverlight application. The customer wanted to make the browser redirect the client to a different URL. As in the current scenario the re-direction was to be made to an internal Silverlight Page within the application we had to use something like-
Response.Redirect();http://localhost/SilverlightSampleTestPage.aspx#/About
In the above example we are trying to redirect to About page.
The application worked liked a charm until it was noticed that when redirected to the About page, the About page would get loaded twice. In order to confirm the same, we used Fiddler which helped us confirm the behavior.
On further investigation it was found that Response.Redirect causes the browser to do a fresh navigation rather than fragment navigation. Due to this Internet Explorer demands for a new copy of the file and hence loads it twice. This is by design.
As the behavior cannot be changed, the following workaround can be used-
1)      Create an initialization parameter, for example initparam.
2)      Assign the page name to which the re-direction is required, to the value attribute, example value="link=/About"
3)      Write the following code to redirect to the new page mentioned in the value attribute

Application.Current.Host.NavigationState = App.Current.Resources["link"].ToString();
4)      This will help us, get the same results as Response.Rediect without the issue of the page loading twice.
Read more: User Interface Support Team Blog