[SalesForce] Get Visualforce page Name in Apex controller

I want get current page name of visulforce page. I have tried this and it works for the page having no query parameters in the URL but it fails in the pages having one or more parameters.
Any workaround will be much helpful. Here is my snippet.

String pageName = ApexPages.currentPage().getUrl().split('apex/')[1];

Thanks in advance!

Best Answer

Another solution

Use $CurrentPage.Name here

in VF page use

<input type="hidden" name="currentvfpage" value="{!$CurrentPage.Name}"/>

And in controller get the hidden input value

String pageName = ApexPages.currentPage().getParameters().get('currentvfpage');
system.debug('-----current page name----'+pageName);
Related Topic