Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    Jul 01, 2020 @ 22:50
    Tim C
    0

    How to get node by id in app_code cshtml

    Not sure how to get a node by id and return its property values in a generic razor file in app_code.

    Any suggestions gratefully received.

  • Mario Lopez 168 posts 958 karma points MVP 4x c-trib
    Jul 01, 2020 @ 23:00
    Mario Lopez
    0

    If you want to create a Razor helper to be reused by your views, I would recommend you to get the node in your views (or controllers in a view model) and pass it to you helper function, instead getting the node in the helper itself.

  • Dhanesh Kumar MJ 165 posts 521 karma points MVP c-trib
    Jul 02, 2020 @ 18:46
    Dhanesh Kumar MJ
    0

    Hey

    use

    var content = Umbraco.Content(1063)

    Or use content services.

  • Tim C 161 posts 528 karma points
    Jul 02, 2020 @ 19:10
    Tim C
    0
    @using Umbraco
    @using Umbraco.Core.Services
    @using Umbraco.Web
    @using Umbraco.Web.Composing
    @using Umbraco.Core.Models.PublishedContent
    @using System.Web
    
    
    @functions{
    
    
    
    
    
    
    
    public static IPublishedContent test(int Id){
        var ttt = Umbraco.Content(Id);
        return null; /* just testing to see if it compiles */
    }...
    

    Returns error:

    Compiler Error Message: CS0234: The type or namespace name 'Content' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)
    
    Source Error:
    
    
    Line 16: 
    Line 17: public static IPublishedContent test(int Id){
    Line 18:    var ttt = Umbraco.Content(Id);
    Line 19:    return null;
    Line 20: }
    

    Do I need to include a different 'using' namespace? (ythis is cshtml in the app_code folder)

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft