Split on QueryString using a macros

Shane Cao asked on August 25, 2020 03:32

I am trying to split my url to return all the query parameters in a string following the "?". I am then going to take that value and append it to the button click that will redirect to another page with the split query parameters. I have tried this in my static HTML web part but can't seem to get the split to work.

<a href="/apps/{% url = QueryString.GetValue("URL"); return url.Split("?")[1]; #%}">Check

Any help or recommendation is greatly appreciated!

Correct Answer

David te Kloese answered on August 25, 2020 15:31

Ah well your not getting the full url.

QueryString.GetValue("URL") does not get the full URL... it gets the value of a parameter called URL.

given: domain.com?URL=123&b=test

the macro QueryString.GetValue("URL") would return 123 not the full URL.


To get just all querystrings you can just use:

{%QueryString%}

given: domain.com?URL=123&b=test

the macro QueryString would return URL=123&b=test so no splitting needed.

0 votesVote for this answer Unmark Correct answer

Recent Answers


David te Kloese answered on August 25, 2020 09:38

Hi,

not sure what you are trying to do? Does your url contain a querystring param that has questionmarks in it? Not sure if that's even allowed but:

QueryString.GetValue("URL") get's the value of a parameter URL.

So if you had

domain.com?url=abc&somethingelse=def

it would get ABC.

But even in case you had:

domain.com?url=a?b?c

you would get a?b?c

If I test with your code I do get the correct values:

Keep in mind the array is 0 based. So your example of [1] gets the 2nd value!

Image Text

Not sure if it's just as a test but keep in mind you're now vulnerable for XSS. For example:

https://i.ibb.co/SPb4gtr/afbeelding.png

0 votesVote for this answer Mark as a Correct answer

Shane Cao answered on August 25, 2020 15:22 (last edited on August 25, 2020 15:23)

Hi David, thank you for the quick response. So basically what I am trying to do is.

So when someone comes to a page on my site the url will look something like this:

https://www.kentico.com/test/?a=test&b=2&c=woot

on this page we have a button. When the user clicks on that button I want to split on the url above which should give me an array of two strings.

[0] - https://www.kentico.com/test/ [1] - ?a=test&b=2&c=woot

I want to then take array [1] and append it to the href of on the tag. So it will end up lookling like this:

https://www.kentico.com/NEWPAGE/?a=test&b=2&c=woot

Does this make sense? In my macros I first try to get the entire URL. then I try to split on the "?" in the url which should then give me an array of 2 strings. I then take array[1] and use it in the tag.

Below is how I have my macros syntax within the tag.

<a href="/apps/{% url = QueryString.GetValue("URL"); return url.Split("?")[1]; #%}">Check

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on August 25, 2020 15:36

Last addition to also handle XSS best to encode the output:

{%(QueryString|(encode)true)%}

0 votesVote for this answer Mark as a Correct answer

Shane Cao answered on August 25, 2020 15:40

Thanks for the help David! I guess I was over thinking this too much hehe :D.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.