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
    Jun 30, 2020 @ 12:56
    Tim C
    0

    Using IUmbacorContextFactory in .cshtml file in App_Code

    Hi

    I am fairly new to low level coding with Umbraco.

    I have worked with Umbraco 7 and in App_Code created a cshtml file to provide some core shared functionality which I would then call from any templates/partial views.

    Eg I would like to have a function where I could pass a node id and return either a single property value or multiple.

    I achieved this in Umbraco 7 using

    public class myDoc {

        private IPublishedContent fetchDoc;
        private int _docID;
    
        public myDoc(int docID)
        {
            _docID = docID;
            fetchDoc = umbracoHelper.Content(docID);
        }
    
        public string docId(){
            return _docID.ToString();
        }
    
    
        public System.Web.HtmlString propertyValue(string strPropertyName)
        {
            var strValue = "";
            try{
                strValue = fetchDoc.GetPropertyValue(strPropertyName).ToString();   
            }
            catch(Exception ex){
                strValue = "Error - invalid document field name (" + strPropertyName + ")";
            }
    
            var nContent = new HtmlString(strValue);
    
            return nContent;
        }
    }
    

    And called this from a template

    However, this doesn't work in Umbraco 8 (at least not the way it did) and my reading says I should now use IUmbracoContextFactory

    However, I don't know how to instantiate this?

    My cshtml code is

    @using Umbraco
    @using Umbraco.Core.Services
    @using Umbraco.Web
    @using Umbraco.Core.Models.PublishedContent
    @using System.Web
    
    
    @functions{
    
    public static IPublishedContent test(IUmbracoContextFactory _umbracoContextFactory,int Id){
        var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
        var content = umbracoContextReference.UmbracoContext.Content.GetById(Id);  
    
        return content;
    
    }
    

    and my template code is

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.HomePage>
        @using ContentModels = Umbraco.Web.PublishedModels;
        @using owClasses;
        @{
            Layout = "standardContentMaster.cshtml";
        }
    
    
        @{
        var idHomePage = 1154;
        var tempvar_0 = @owRazorExtend.test(Umbraco,idHomePage);
        }
    
    @section Body
    {
    
    <h1>Test</h1>
    
      <h4>@tempvar_2</h4>
    }
    

    and I get error

    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1503: Argument 1: cannot convert from 'Umbraco.Web.UmbracoHelper' to 'Umbraco.Web.IUmbracoContextFactory'

    Source Error:

    Line 9: @{ Line 10: var idHomePage = 1154; Line 11: var tempvar_0 = @owRazorExtend.test(Umbraco,idHomePage); Line 12: } Line 13:

    so clearly I am passing the wrong reference to test.

    Can any one advise?

    Is there any documentation on this stuff eg the class, methods and properties in the various namespaces?

    Many thanks

Please Sign in or register to post replies

Write your reply to:

Draft