Adding a link on page with current URL (including hash) in a querystring
I don't think this can possibly be improved.
The location hash may be changed by the client after the page is loaded,
by clicking on local anchors.
This is not visible by the asp.net,
and fully visible by the client-side javascript,
in the value of location.href.
And since you require the complete URL to be included as part of the query string,
it must be encoded with encodeURIComponent,
which you correctly did.