I have made a simple Api controller on my umbraco website, that simply searches some nodes for some properties and returns the content from those properties in json. One of the properties is an umbraco grid. Technically that is also json, and I could successfully get the content with some elaborate JObject querying. But from the view side there is access to a "GetGridHtml" helper function that does that for you. So my question is, how do I achieve that in a controller? I got the node as IPublishedContent which incidentally has that helper function, but it wants an IHtmlHelper which isn't available in this context. So what do I do?
[HttpGet]
[Route("api/myapp/version/")]
public IActionResult GetLatestMyAppVersion()
{
var releaseNote = _umbracoHelper.ContentAtXPath("//releaseNoteList").First().Children().OrderByDescending(x => x.Value<DateTime>("releaseDate")).First();
var ver = releaseNote.Value<string>("myAppReleaseVersion");
var date = releaseNote.Value<DateTime>("releaseDate");
var desc = releaseNote.GetGridHtml(?????, "mainContent", "Clean"); // this is where IHtmlHelper is needed
return Ok(new { Application = "MyApp", Version = ver, ReleaseDate = date.ToString("yyyy-MM-dd"), Description = desc });
}
Ah, sorry, I think I understand it now. Short version is there is no method built into the Umbraco API where I can get the HTML helper? So I can not use GetGridHtml without generating one myself? Which seems really overkill and bad practice, am I right?
IHtmlHelper is a .net core interface and nothing to do with Umbraco, it is bad .Net practice to use IHmHelper outside of views. So without instantiating a fake one you can't use GetGridHtml in a controller or class.
What exactly did you want to do with the html? Why not just return a partialView which could use the GetGridHtml?
Well Its not because I WANT to use ihtmlhelper, which is also why I was asking around here.
As explained in the OP, I have made an API that fetches a few key details from some umbraco nodes and sends that data back as a simple piece of JSON. One of those properties from those nodes is an umbraco grid. That is stored as JSON and I can get the grid data in a JObject (not in the example I have given). As I was making it, it just seemed more correct to use GetGridHtml. But I understand now that is off the table.
So either getting the grid json "manually" is the correct path, or there is some other more "clever" method.
Using GetGridHtml in a controller in Umbraco 10
I have made a simple Api controller on my umbraco website, that simply searches some nodes for some properties and returns the content from those properties in json. One of the properties is an umbraco grid. Technically that is also json, and I could successfully get the content with some elaborate JObject querying. But from the view side there is access to a "GetGridHtml" helper function that does that for you. So my question is, how do I achieve that in a controller? I got the node as IPublishedContent which incidentally has that helper function, but it wants an IHtmlHelper which isn't available in this context. So what do I do?
HtmlHelper is not really meant to be used outside of views, you could try the below link for a solution
https://github.com/aspnet/Mvc/issues/7321
Well ok, then what do I do instead? There is no proposed solution in the link you provided.
the initial post in the link proposes the solution you could try.
Ah, sorry, I think I understand it now. Short version is there is no method built into the Umbraco API where I can get the HTML helper? So I can not use GetGridHtml without generating one myself? Which seems really overkill and bad practice, am I right?
IHtmlHelper is a .net core interface and nothing to do with Umbraco, it is bad .Net practice to use IHmHelper outside of views. So without instantiating a fake one you can't use GetGridHtml in a controller or class.
What exactly did you want to do with the html? Why not just return a partialView which could use the GetGridHtml?
Well Its not because I WANT to use ihtmlhelper, which is also why I was asking around here. As explained in the OP, I have made an API that fetches a few key details from some umbraco nodes and sends that data back as a simple piece of JSON. One of those properties from those nodes is an umbraco grid. That is stored as JSON and I can get the grid data in a JObject (not in the example I have given). As I was making it, it just seemed more correct to use GetGridHtml. But I understand now that is off the table. So either getting the grid json "manually" is the correct path, or there is some other more "clever" method.
Any solution? I'm needing the same thing. Exposing grid and/or blocklist in an API JSON return.
The Grid content is already a json value. So you should just be able to pass it to the Description property?
is working on a reply...