Skip to content

Working with a Headless approach

Simon Yohannes edited this page Jan 12, 2020 · 10 revisions

Puck is pretty flexible, you don't need to use razor views if you don't wan't to. You could instead return JSON of the current page or other queried content.

By default, there is a catch-all endpoint mapped to the HomeController Index action. The code for this mapping is in Startup.cs:

endpoints.MapControllerRoute(
   name: "default",
   pattern: "{**path}"
   ,defaults: new { controller = "Home", action = "Index"}
);

HomeController inherits from puck.core.Controllers.BaseController and if you look at the Index action:

public IActionResult Index()
{
    return base.Puck();
}

all it does is call the inherited Puck action which will get the current page and render a view. if you wanted to go headless, you could just return the current page as JSON:

public IActionResult Index()
{
    var currentPage = QueryHelper<BaseModel>.Current();
    return Json(currentPage);
}

this is a simple example but in a real world scenario you'll likey want to use the QueryHelper to get multiple pieces of content and compose them into a ViewModel to return to the client.