locked
How to clear a query string from browser uri? RRS feed

  • Question

  • In my Silverlight 3.0 application I am handling a query string in OnNavigatedTo Event

    if (HtmlPage.Document.QueryString.Count != 0)
    {
        if (HtmlPage.Document.QueryString.ContainsKey("UserId"))
         {
                
    EmailConfirmationMessageBox newConfirmationWindow = new EmailConfirmationMessageBox();
                
    newConfirmationWindow.UserGuid = HtmlPage.Document.QueryString["UserId"];
               
    newConfirmationWindow.Show();
        
    }
    }

    The problem I am facing is that when I navigate to another page the query string is not getting cleared. i.e. the on the browser address bar still shows my query string.

    How can I clear the Query string from my code?

    I have tried using NavigationContext.QueryString.Clear(); and HtmlPage.Document.QueryString.Clear(); with no success. Help!

    Tuesday, July 28, 2009 2:24 AM

All replies

  •  Correct me if I am wrong but you can not change the query string until you hit the server. You can change the bookmark but to change query you need a new request to server that ultimately refreshes the page. Silverlight navigation is client side so it might not be possible to clear it.

    Tuesday, July 28, 2009 2:35 AM
  • Hi,

    Even I wanted to clear the Query String when I click on Logout button. Here is the code which I used :

    if (HtmlPage.Document.DocumentUri.Fragment.Length > 0)

    {

    string newUri = HtmlPage.Document.DocumentUri.OriginalString;

    newUri = newUri.Replace(HtmlPage.Document.DocumentUri.Fragment, "");

    HtmlPage.Window.Navigate(new Uri(newUri));

    }

    HTH Smile

    Tuesday, July 28, 2009 2:53 AM
  • @tanmoy: Exactly!

    Wednesday, July 29, 2009 5:02 AM
  • Hi,

    Even I wanted to clear the Query String when I click on Logout button. Here is the code which I used :

    if (HtmlPage.Document.DocumentUri.Fragment.Length > 0)

    {

    string newUri = HtmlPage.Document.DocumentUri.OriginalString;

    newUri = newUri.Replace(HtmlPage.Document.DocumentUri.Fragment, "");

    HtmlPage.Window.Navigate(new Uri(newUri));

    }

    HTH Smile

     That can be a work around if you want to refresh your page. In this way we are actually loading the complete page again. There should be some way to clear query string without actually refreshing the page.

    Wednesday, July 29, 2009 5:22 AM
  • Hi,

    The reason you can not change the querystring without refreshing is because of a security restriction.

    Query string is considered as a part of the url itself. If the browser lets you do it then a attacker might try in the following way...

    1. Attacker domain is abcd.com

    2. Now suppose she can change it to  google.com

    3. Now the javascript or anything she runs on the page will think it is coming from google.com and remove the restriction that you normally have with same origin policy.

    Changing only the Query string might not that much dangerous but as a user you can change/run any Javascript you want. (Try fiddler). Now if it is allowed then you can change the querystring and the server scripts that runs based on querystring values will be misleaded.

    And this is the reason bookmark (#) is there. This is a nice thing that you can change from client side easily and you can rely on it for these type of situations.

    Saturday, August 1, 2009 3:16 AM
  • Hi! I understand why HTML.Document is read only. But the issue still stands, and that is how to clear the Query String collection as it is firing my check again and again which i do not want.

    Besides, I happen to find a work around. I created a property in my App.xaml which keeps a track if that page is a post back. Basically, a bool value equivalent to my IsPostBack in traditional Asp.Net.

    So, every time I come back to my page I see if its a post back and then I disable my check of query string.

    I wonder if there is a better way out? But this solves my problem for now :)
    Saturday, August 1, 2009 9:17 AM
  • I wonder if there is a better way out?

    I don't know how much of the app is under your control, but is it possible NOT to use the query string in the first place? In earlier versions of my applications, I ran into problems like this, and the solution was to replace query strings with bookmarks which can be modified client side without refreshing the page.

    Saturday, August 1, 2009 5:00 PM
  • the solution was to replace query strings with bookmarks

     

    Hi!

    Thanks for the suggestion.

    Now to be specific in my application there is a use case of "Email Verification" of a new customer where I am sending a User Id in querystring in my website link.

    When the site opens I have a check on intialization of HomePage if there is querystring contining Key UserId please do the verification of Email. You can refer to the code i mentioned above.

    How can I achive this using bookmarks?
    Saturday, August 1, 2009 11:27 PM
  • Hi,

    HtmlPage.Document.QueryString and NavigationContext.QueryString doesn't refer to the same string.

    For example

    http://localhost/default.aspx?query=server#/Home?query=client

    • "query=server" part is server querystring, it's readonly for client. You could get this string by HtmlPage.Document.QueryString.
    • "query=client" part is silverlight navigation querystring, it can be read/write by NavigationContext.QueryString.

    In your scenario, we could use the second kind querystring, when navigating to other silveright page, the querystring could be add/modify/remove as you wish.

    Thanks,

    Monday, August 3, 2009 11:58 PM
  • Thanks for the reply.

    When I was devloping the application in Silverlight 3.0 Beta the url for each frame use to come like

    http://localhost/default.aspx/#Home.xaml

    So what i use to do was I use to append by required query string to the url like http://localhost/default.aspx/#Home.xaml?UserId=12345 and I use to find it in Navigation.Context.QueryString.

    But now, correct me if I am wrong, in Silverlight 3.0 they have given an HTTP handler which basically shows the Url as http://localhost/default.aspx only. and hence my Navigation.Context.QueryString shows Zero count even if I am opening link as

    http://localhost/default.aspx/#Home.xaml?UseId=12345
    OR
    http://localhost/default.aspx?UseId=12345
    So, I am unable to user Navigation.Context.QueryString.

    Also, I would like to revisit my requirement. I need to mail the url containing the query string to the user and I have to check the querstring value on intialization of my homepage.
    Wednesday, August 5, 2009 5:32 AM
  • Hi Mog...

    I'm revisiting this same issue :S

    I started out with a server query string, which works fine but I also need to be able to remove it.  I'm a bit confused about how to implement it client side.  Can you either give an example of a url that contains a client side query string, or direct me to a tutorial?

    Thanks!

    Wednesday, June 9, 2010 5:43 PM