Umbraco 7 mvc how to get current page id

KleberBH picture KleberBH · Apr 22, 2014 · Viewed 11.7k times · Source

I have a custom controller to send message. So I need to get the value of property field name and alias = "email", this will be used to send the email to.

this code below works

 var id = umbraco.uQuery.GetNodeByUrl("/contact-us");
 IPublishedContent root = Umbraco.TypedContent(id.Id);
 return root.GetProperty("email", true).Value.ToString();

However the problem here is if the page name changes, the url will change and the code will break.

So how can I change the code above to get the current page id and insert it here (???);?

I think the code should be something like this:

 IPublishedContent root = Umbraco.TypedContent(???);
 return root.GetProperty("email", true).Value.ToString();

Any help will be apprecciated

Answer

antao picture antao · Apr 22, 2014

Your solution will bring a problem if you have more than 1 'ContactUs' node, or none. Then you don't know which one is going to be get.

(Actually it's the first one found in the node tree, but then someone can change the order of them...)

Is your controller a Surface controller? You can just do this:

IPublishedContent currentNode = Umbraco.TypedContent(CurrentPage.Id)