Serving API
Orchard Core simplifies creating API endpoints by leveraging ASP.NET Core's MVC framework. We can define a controller with specific methods, and Orchard Core will automatically expose those as endpoints.
Let's start a simple controller in the CMS core project with the following code.
using Microsoft.AspNetCore.Mvc;
namespace OCBC.HeadlessCMS.Controllers;
[ApiController]
[Route("api/v1/product")]
public class ProductController : Controller
{
[HttpGet("product-info")]
public IActionResult GetProductInformation()
{
return Ok(new { message = "Hello from Orchard Core!" });
}
}We should be seeing the message being printed when we visit the endpoint.
Since we are building this serving API, so we need a way to retrieve the content items from Orchard Core. In Orchard Core, we can use IOrchardHelper in to interact with content items and other Orchard Core services easily. IOrchardHelper provides utility methods to retrieve content items, execute queries, and work with shapes, making it incredibly useful for API development.
There are a few key methods in IOrchardHelper, as explained in the following table.
GetContentItemByIdAsync(string contentItemId)
Retrieve a specific content item by its ID.
QueryContentItemsAsync(Func<IQuery, IQuery> query)
Query content items using LINQ-like syntax.
ImageResizeUrl(string imagePath, int? width = null, int? height = null, ResizeMode resizeMode = ResizeMode.Undefined)
Returns a URL with custom resizing parameters for an existing image path.
SanitizeHtml(string html)
Sanitises an HTML string.
For example, to retrieve all the published product information with "ProductInformation" as the ContentType, we will need to make use of the QueryContentItemsAsync, as demonstrated below.
With this, the output of the endpoint should be a JSON as follows.
The list above will include both the published and draft content items. If we only want to show the published ones, we can do as follows.
If we have the ContentItemId, we can use it to retrieve the corresponding content item too.
So, when we visit the endpoint /api/v1/product/product-info/4fdpjjm5cgqz3y5zzaz72ccp4a, we will receive the following JSON output.
Last updated