querystrings and friendly urls

Member since: 2006
 
Hi,

I'm having some difficulties creating url's that includes querystring parameters. The project I'm working with uses friendly url's. So, my first question is: how do I build a correct link that includes querystring parameters?

My second question regards the action off grabbing the querystring parameters on the receiving page.

Thanks for your help.

#27618 Feb 04, 2009 15:02
  •  
    Don't use ?ID= querystring beacause EPiServer uses that for the friendly urls. Otherwise you should be able to use querystrings.
    #27627 Feb 04, 2009 15:28
  • Member since: 2004
     

    1. EPiServer.UriSupport.AddQueryString(CurrentPage.LinkURL, "querystringname", "querystringvalue")

    For more query string parameters than one, use UriSupport.BuildQueryString(NameValueCollection).

    2.  Request.QueryString["querystringname"]

    Hope it helps,
    /Marten

    #27640 Edited, Feb 04, 2009 20:03
  • Member since: 2006
     

    Yeah! Thanx, it works just perfect.

    #27645 Feb 05, 2009 10:37
  • Member since: 2009
     

    I apologize for posting in such an old thread, but its the only one I've found through a couple of hours of searching that deals directly with my problem.

    The Solution offered looks good, but i don't understand where the indicated piece of code goes? It seems to me like the statement is creating a link to the current page, with parameters.

    I would love to se a more complete example

    Furthermore, I would like to know if adding querystring (or any kind of) parameters to a link can be done through the link tool in the editor. I cant seem to be able to do this myself.

     

    br

    Jørgen 

    #29563 May 05, 2009 12:56
  • Member since: 2004
     
    Hello,

    It's just an example, and not meant to be taken literally. Maybe a better example would be something like below. This code is executed code-behind when clicking a top menu quicksearch linkbutton called "SearchButton". It takes the content of a TextBox called QuickSearch and returns its value as a querystring to a designated search page (The "SearchPage" property).


    protected void SearchButton_Click(object sender, EventArgs e)
    {
       string searchString = Server.UrlEncode(QuickSearch.Text.Trim());
       PageReference searchPageRef = CurrentPage["SearchPage"] as PageReference;

       if (string.IsNullOrEmpty(searchString) || searchPageRef == null)
         return;

       string searchPageUrl = UriSupport.AddQueryString( GetPage(searchPageRef).LinkURL, "q", searchString );

       Response.Redirect(searchPageUrl, true);
    }

    If you still don't understand the concept, instead try to describe the problem you are facing.

    Regarding your second question: I don't think there is a built-in functionality to include query strings in the editors link tool. You (i.e. The editorial person) may have to do this yourself in the HTML mode after you included the link, or build your own plugin that overrides the current link tool functionality, which may seem like a bit of overkill.

    Hope it helps,
    /Marten

    #29564 May 05, 2009 14:10
  • Member since: 2006
     

     

    I'm having a bit of a problem regarding the UriSupport.BuildQueryString method.  I have this link to which I want to add three parameters. I do the following:

     

    NameValueCollection myCol = new NameValueCollection();

                myCol.Add("Comment", "false");

                myCol.Add("EditMode", "true");

     

    string redirect = CurrentPage.LinkURL + UriSupport.BuildQueryString(myCol);

     

    When I click my link the page can't be displayed. I've altered the code above several times but with no luck. Does anybody have any solutions to my problem?

    #29846 May 19, 2009 9:55
  • Member since: 2008
     
    Somehow I had to add the first QueryString element twice.
    Like this:

    NameValueCollection myCol = new NameValueCollection();
    myCol.Add("SkipCount", Count.ToString());
    myCol.Add("SkipCount", Count.ToString());
    myCol.Add("Year", DropDownYear.SelectedIndex.ToString());
    myCol.Add("Month", DropDownMonth.SelectedIndex.ToString());

    string pagelink = CurrentPage.LinkURL + UriSupport.BuildQueryString(myCol);

    #31097 Jul 02, 2009 14:40
  •  

    The reason you have to add the first parameter twice is that LinkURL already has query string parameters and the BuildQueryString method doesn't add a leading question mark. You will then end up with a url where the first SkipCount has been added to the parameter value of the previous parameter like this:

    /template.aspx?id=3&epslanguage=enSkipCount=1&SkipCount=...

    I'd recommend using the EPiServer.UrlBuilder to merge query strings

    NameValueCollection myCol = new NameValueCollection();
    myCol.Add("SkipCount", "1");
    myCol.Add("Year", "2009");

    UrlBuilder urlBuilder = new UrlBuilder(CurrentPage.LinkURL);
    urlBuilder.MergeQueryCollection(myCol);
    string pageLink = (string)urlBuilder;

    Note that the explicit cast to a string of the urlBuilder is important.

    #31105 Edited, Jul 02, 2009 17:33
  • Member since: 2008
     
    Stefan:
    It actually worked with dbl-first-parameter.
    But sure, your suggestion is much cleaner code.
    Thanks a lot
    #31106 Jul 03, 2009 7:02
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
Please login to post a reply