Copied to clipboard

Flag this post as spam?

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


  • Qaisar 30 posts 76 karma points
    Sep 12, 2019 @ 07:20
    Qaisar
    0

    Umbraco headrest/headless V8

    I am trying to implement Umbraco headrest project from Matt Brailsford. I have cloned the project from github and followed the instructions for setting it up for the Umbraco v8. I setup default umbraco starter kit and tried the basic setup for headrest with mapping from Home node to HomePageViewmodel as in

    https://github.com/mattbrailsford/umbraco-headrest/tree/wip/v8

    I am getting an error : InvalidOperationException: Don't know how to map Umbraco.Web.PublishedModels.Home to test.headrestv8.HomePageViewModel.

    Here is the code for Configuration component is:

    public class HeadRestConfigComponent : IComponent
    {
        private readonly HeadRest _headRest;
    
        public HeadRestConfigComponent(HeadRest headRest)
            => _headRest = headRest;
    
        public void Initialize()
        {
            _headRest.ConfigureEndpoint(new HeadRestOptions
            {
                ViewModelMappings = new HeadRestViewModelMap()
            .For(Home.ModelTypeAlias).MapTo<HomePageViewModel>()
            });
            }
    
        public void Terminate() { }
    }
    
    public class HeadRestConfigComposer : ComponentComposer<HeadRestConfigComponent>
    { }
    

    Right now i am only trying to map one field. The code for MapDefinition is:

    public class TestHeadRestMapDefinition : IMapDefinition
    {
        public void DefineMaps(UmbracoMapper mapper)
        {
            mapper.Define<Home, HomePageViewModel>(
                (source, context) => new HomePageViewModel(),      // Constructor function
                Map);            
        }
        private void Map(Home source, HomePageViewModel target, MapperContext context)
        {
            target.ColorTheme = source.ColorTheme;
        }
    }
    
    public class TestHeadRestMapDefinisionComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.WithCollectionBuilder<MapDefinitionCollectionBuilder>()
                .Add<TestHeadRestMapDefinition>();
        }
    }
    

    The code ViewModel is:

    public class HomePageViewModel
    {
        public string ColorTheme { get; set; }
    
    }
    

    I can't figure out whats going wrong or did i set it up in a wrong way? Any help would be really appreciated.

  • Qaisar 30 posts 76 karma points
    Sep 16, 2019 @ 12:48
    Qaisar
    0

    I am still strugling with a solution for this error. I found another error message "Method may only be called on a Type for which Type.IsGenericParameter is true."

    at

    ((System.RuntimeType)((System.Reflection.RuntimeMethodInfo)($exception).TargetSite).DeclaringType).DeclaringMethod

    Is there any one who has faced the similar error? or any thoughts guys ?

  • Neil Hodges 309 posts 915 karma points
    Apr 15, 2020 @ 09:29
    Neil Hodges
    0

    Hi Qaisar

    I'm hitting the exact same problem after setting it up. Did you ever find a solution to this?

  • Qaisar 30 posts 76 karma points
    Apr 15, 2020 @ 12:23
    Qaisar
    0

    Hi Neils

    I got it fixed by using the same naming convention. You can also try to look at the following project from Matt, its based on headrest.

    https://github.com/mattbrailsford/nullorwhitespace-backend

  • Neil Hodges 309 posts 915 karma points
    Apr 15, 2020 @ 12:36
    Neil Hodges
    0

    Hi Qaisar

    With a little more digging it seems my naming conventions were wrong, namely calling a class - HeadRestComposer

    Changing my Composer Class name so it didn't crash with HeadRestComposer seems to have fixed it.

    I've not seen that NullOrWhiteSpace project before, I'll look into it, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft