'Umbraco 8.17 How to get page id of a different page?

I needed to get to a different page if certain conditions were meet within my SurfaceController. If you have the page ID of the page you need to get to this is ease. The problem was I did not want to hard code a page id in the action method in the controller. I had a lot of problems trying to figure it out, so I thought I would provide my solution in case someone else is having the same issue.

                int homePageID = 0;
                try
                {
                    homePageID = Umbraco.ContentAtXPath("//home").FirstOrDefault().Id;
                }
                catch(Exception e) { }
                if(homePageID > 0)
                {
                    return RedirectToUmbracoPage(homePageID);
                }
                else
                {
                    return CurrentUmbracoPage();
                }  

Note that for this line "homePageID = Umbraco.ContentAtXPath("//home").FirstOrDefault().Id;" you use the page alias not the page name.

I hope this helps.



Solution 1:[1]

int homePageID = 0;

try
{
    homePageID = Umbraco.ContentAtXPath("//home").FirstOrDefault().Id;
}
catch(Exception e) { }
if(homePageID > 0)
{
    return RedirectToUmbracoPage(homePageID);
}
else
{
    return CurrentUmbracoPage();
}  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney