Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luyolo Mgodi 71 posts 120 karma points
    Jul 02, 2015 @ 22:08
    Luyolo Mgodi
    0

    Get Current Page/Node

    Hi,

    I am using Umbraco API Controller and I'm trying to get the current page so that I can access the properties on in the controller. But Umbraco throws a "No current node" or a null exception when I use "Node.GetCurrent". I tried everything I found in the internet but throws the same error. However, If I use IPublishedContent currentPage = Umbraco.TypedContent(1079);, as you can see, I hard-coded the NodeId, I do find my page/Node. But I do not want to hard-code my page/nodeId because I need it to be dynamic. Any help would be great.

    Thanks.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 03, 2015 @ 07:05
    Dennis Aaen
    0

    Hi Luyolo,

    I was thinking if you can do something like this:

    IPublishedContent currentPage = Umbraco.TypedContent(CurrentPage);
    

    Will this do what you are trying to archive.

    Hope this helps,

    /Dennis

  • Luke 110 posts 256 karma points
    Jul 03, 2015 @ 08:19
    Luke
    0

    Hi,

    If you are using Surface Controller you can use:

    var page = CurrentPage;
    

    If you are using api controller then its not possible - you need to send the id as part of the GET or POST and then use your method above, ie:

    [HttpGet]
    public bool DoSomething(int id)
    {
           IPublishedContent currentPage = Umbraco.TypedContent(id);
    }
    
  • Luyolo Mgodi 71 posts 120 karma points
    Jul 03, 2015 @ 08:24
    Luyolo Mgodi
    0

    Hi Luke,

    Im using the UmbracoApiController.

    IPublishedContent currentPage = Umbraco.TypedContent(id);
    

    That is the problem I'm having? How to get that id?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 03, 2015 @ 08:31
    Dennis Aaen
    0

    Hi Luyolo,

    What if you do something like this

    IPublishedContent currentPage = Umbraco.TypedContent(CurrentPage.Id);
    

    Hope this helps,

    /Dennis

  • Luyolo Mgodi 71 posts 120 karma points
    Jul 08, 2015 @ 16:55
    Luyolo Mgodi
    0

    Hi Dennis,

    I tried that, but it does not work. That will work if I use "SurfaceController" but not when I use UmbracoApiController.

     public class MyController : UmbracoApiController
    

    That is what I want to use and not

    public class MyController : SurfaceController
    

    Thanks,

    Luyolo

  • Luyolo Mgodi 71 posts 120 karma points
    Jul 08, 2015 @ 18:45
    Luyolo Mgodi
    0

    Also, 1 think I should mention. I am using angularjs to do call to my controller.

    This is the exception I get

    Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request
    
Please Sign in or register to post replies

Write your reply to:

Draft