Copied to clipboard

Flag this post as spam?

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


  • Liam Prowse 6 posts 76 karma points
    Apr 28, 2022 @ 20:05
    Liam Prowse
    0

    Umbraco 9 Blazor Components

    I've been having a play arond trying to get blazor components to work with Umbraco 9.

    I'm trying to pass in an umbraco generated model to the blazor component like so

    @(await Html.RenderComponentAsync<CounterPartialClass>(RenderMode.ServerPrerendered, new { Model = Model}))
    

    However i get the following error

    JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

    Has anyone achieved this or found a way around it?

    Thanks in advance

  • Rafael Montero 1 post 71 karma points
    Jul 01, 2022 @ 17:23
    Rafael Montero
    0

    Hi, how are you? Did you manage to solve it? I am having the same problem.

  • Pavlos 1 post 71 karma points
    Jul 04, 2022 @ 11:52
    Pavlos
    0

    As the exception details, you could set your JsonSerializerOptions to support cycles, how you would go about this in Umbraco is beyond me though.

    An alternative method would be to map the model to your own models which don't include circular references (which umbraco generated classes contain, eg: parent->child->parent->child etc..)

    Another option would be to query your content in the Blazor component (OnInitializedAsync) based on simple type parameters that you pass to your component. That way no object cycles need to be serialized.

    It's hard to provide examples with no code provided, but either of these methods should prevent this issue.

  • Corné Hoskam 80 posts 587 karma points MVP 3x c-trib
    Jul 08, 2022 @ 10:48
    Corné Hoskam
    0

    Hi Liam,

    I agree with Pavlos' suggestions! With Blazor it's quite tricky to directly reference Umbraco models, so it would probably be better to use your own models that you pass into your Blazor component so that you don't have unnecessary references!

    This also means that your Blazor components will be less dependend on the models that are generated by Umbraco!

    Hope this helped!

Please Sign in or register to post replies

Write your reply to:

Draft