You need to sign in to do that
Don't have an account?
How to get full host name in Apex?
I'm trying to build the url for a page to be called via Ajax in a Salesforce Sites page which is in the format:
http://sitename-developer-edition.na3.force.com/extension
When I do {!$Page.myPage}" in Visualforce it it brings up the full correct reference to the page:
/extension/PackageName__pageName
This works great.
However, I cannot find out how to correctly find out the "extension" through native Apex code.
ApexPages.currentPage().getHeaders('Host') returns only the first part, "sitename-developer-edition.na3.force.com" without the extension, and there is no other information in any of the Headers element which contains it (i.e. Accept-Charset, Accept-Encoding, Accept-Language, CipherSuite, Connection, Host, Keep-Alive, User-Agent, X-Salesforce-Forwarded-To, X-Salesforce-SIP).
Advice?
The way I would approach this is to write a little bit of javascript and feed window.location.href (or any other property of window.location you like) to an apex:actionFunction.
Here's some sample code which will feed the entire url to the controller at the load of the VF page.
VF page:
Apex Controller:
You can adapt this approach to anywhere else you can invoke javascript, (like on command links / buttons). Hope this helps.
I suggested the same thing in another thread on a similar topic. The problem is that the page has to finish loading first and if I want to provide another link to a different page on that page I cannot (unless I do a similarly quirky thing and load the extension into the DOM and populate the correct page extension). So I am still searching for another solution.
That said, this works assuming you have the actionFunction listed above:
This is much easier and cleaner though:
some how I missed the getPrefix method since I was looking in the wrong docs...
Ahh very nice.