Copied to clipboard

Flag this post as spam?

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


  • David Zweben 231 posts 688 karma points
    Feb 11, 2018 @ 17:54
    David Zweben
    0

    Help implementing a custom C# class in App_Code?

    Hi,

    I am trying to set up a (basically empty) custom class in the App_Code folder, to use as a starting point for some code I'd like to be able to reuse in my templates.

    Right now I have the following in my App_Code file:

    using umbraco.cms.businesslogic;
    using Umbraco.Core.Models;
    using System.Linq;
    using System.Globalization;
    
    namespace MyNamespace
    {
        public class TranslationHelper
        {
            public IPublishedContent GetTranslatedContentNode(IPublishedContent Model, string currentCulture)
            {
                IPublishedContent contentNode = Model;
    
                return contentNode;
            }
    
        }
    }
    

    ...and the following in my template:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.TextPage>
    @inherits MyNamespace.TranslationHelper
    
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Homepage.cshtml";
    }
    
    
    @{
        var contentNode = new MyNamespace.TranslationHelper.GetTranslatedContentNode(Model);
    }
    

    But I am getting the following error:

     CS0115: 'ASP._Page_Views_TextPage_cshtml.Execute()': no suitable method found to override
    

    Can anyone see what I'm doing wrong? Any help would be greatly appreciated.

    Thanks,
    David

  • Nigel Wilson 939 posts 2061 karma points
    Feb 11, 2018 @ 18:24
    Nigel Wilson
    0

    Hi David

    2 questions:

    1. Should it be Model.Content in the method call within your view.
    2. Your method takes 2 parameters yet you are only passing 1 from the view.

    Cheers

    Nigel

  • David Zweben 231 posts 688 karma points
    Feb 11, 2018 @ 18:28
    David Zweben
    0

    Looks like I figured it out myself with a little more searching on Our.

    This was the thread that helped me out: https://our.umbraco.org/forum/developers/razor/44015-Access-App_Code-classes-in-Razor

    And here's the code I ended up with:

    .CS file in App_Code:

    using umbraco.cms.businesslogic;
    using Umbraco.Core.Models;
    
    namespace MyNamespace
    {
        public class TranslationHelper
        {
            public IPublishedContent Title { get; set; }
            public static IPublishedContent GetTranslatedContentNode(IPublishedContent model)
            {
                IPublishedContent contentNode = model;
    
                return contentNode;
            }
    
        }
    }
    

    Template:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using MyNamespace;
    @{
        Layout = "Homepage.cshtml";
    }
    <p>@TranslationHelper.GetTranslatedContentNode(@Model)</p>
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies