In this post, I am going to share the information How To Get Current Page URL from Apex code in Salesforce. So we need to use Apexpages.currentPage() methods.

 

 Get Current Page URL

‘getHeaders()’ : It return a map of the request headers.where the key string contains the name of the header, and the value string contains the value of the header.
After that get the ‘Host’ key value from that map

‘getUrl()’ : It returns the relative URL associated with the PageReference when it was originally defined, including any query string parameters and anchors.

Apex Code :

public with sharing class pageurlclass{

         /**
         * Webkul Software.
         *
         * @category  Webkul
         * @author    Webkul
         * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
         * @license   https://store.webkul.com/license.html
         */

    public  String  headerdata{get;set;}
    public string urlvalue{get;set;}
    public string url{get;set;}
        
    public pageurlclass(){
        
         headerdata= ApexPages.currentPage().getHeaders().get('Host');
        
         urlvalue=Apexpages.currentPage().getUrl();
        
         url='https://' + headerdata+ urlvalue;
    }
  

}

Visualforce Page : Create visualforce page with name Pageurl

      
  1.  {!headerdata}
 
  2.  {!urlvalue}
 
  3.  {!url}

Output

1. c.ap2.visual.force.com

2. /apex/Pageurl

3. https://c.ap2.visual.force.com/apex/Pageurl

 

这是小喵从其他网站上复制过来的,

因为有时需要用到所以记录一下,

小伙伴们觉得有用的话也可以记录一下哦!

(^_^)~喵~!!