Sitecore API for Adding & Retrieving Query Strings

Whenever we start any new project we tend to write some Utilities class with frequently used methods like GetItem, EditItem, etc. and also, adding & retrieving query strings using UriBuilder/HttpUtility.ParseQueryString API.

Sitecore uses query strings like sc_mode, sc_lang etc. This got me a thinking.

Sitecore should have come up with its own methods for adding & retrieving query strings.

Luckily we have such methods in Sitecore.Web.WebUtil class, syntax below,

Sitecore.Web.WebUtil.AddQueryString(<em>url</em>, <em>key1</em>, <em>value1</em>, <em>key2, value2</em>)

AddQueryString method will encode the query string values (in case of special characters), so url encoding using HttpUtility.UrlEncode is not required. In case of decoding, ExtractUrlParm & ParseQueryString methods will help.

Hope this will save you some lines of code and time.

Originally Posted at:

Leave a Reply

Your email address will not be published. Required fields are marked *