Rendering ContentItem to Json #11067
-
I'm new to the OrchardCore side of an existing project, so I'm not up to speed on all the correct terminology and best practices. End Goal: I want to render my content item as JSON. Problem: We are calling the following, and this does not call all the Display Drivers for our widgets. var model = await DisplayManager.BuildDisplayAsync(context.Source, UpdateModelAccessor.ModelUpdater)
var item = model.Properties.ContainsKey("ContentItem") ? model.Properties["ContentItem"] as ContentElement : null;
return item?.Content as JObject; Reason: The reason I need to run the DisplayDrivers is that we tweak some property values in there. We see these when we render to HTML, but not when we render to JSON. I see that currently what happens in our HTML Razor flow is that we render the root level .cshtml and that loops through the children and calls Build and Display on each of those children, which in turn does the same on its children in their cshtml templates. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
You can use this nice tool : https://docs.orchardcore.net/en/latest/docs/reference/modules/Contents/#razor-console-log |
Beta Was this translation helpful? Give feedback.
-
Using They do not modify the source content item json. So you can't use display drivers to produce json. To create modified JSON output, you could use a decoupled approach, cloning the json of the content item, and modifying it, as you wish |
Beta Was this translation helpful? Give feedback.
-
You could use |
Beta Was this translation helpful? Give feedback.
-
@dannoh for changing value and calculating I’d suggest to use content handler instead of display driver |
Beta Was this translation helpful? Give feedback.
Using
DisplayDrivers
produces views, either liquid or razor. The models they produce are generally aViewModel
.They do not modify the source content item json.
So you can't use display drivers to produce json.
To create modified JSON output, you could use a decoupled approach, cloning the json of the content item, and modifying it, as you wish