Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Umbraco 7 mvc how to get current page id

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

like image 579
KleberBH Avatar asked Dec 15 '22 23:12

KleberBH


1 Answers

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)
like image 134
antao Avatar answered Jan 18 '23 05:01

antao