As you may already know that SharePoint uses Source parameter in query string to take you back to the page from where you came.
It will return you properly encoded string to pass as Source parameter value. Following is the logic inside this function:
- Try to retrieve the value Source parameter from current query string.
- if Source parameter found then return it by properly encoding and making sure it is safe to navigate.
- If there is no Source parameter in Query string then
- Check if defaultSource has a non empty value,
- if yes then return it by properly encoding and making sure it is safe to navigate.
- Otherwise, return the current page url by properly encoding and making sure it is safe to navigate.