Controllers

Last Updated: 2018-11-14

For detailed information on how the routing of Piranha works, please refer to Routing.

As Piranha in fact only provides a middleware pipeline for rewriting requests to the underlying web framework you will need to define your own code for handling the requests. If you've created your project from one of the available Templates you will have a default controller created for you in Controllers/CmsController.cs.

Default Routes

All content types that don't explicitly specify a route will be sent to the following actions.

using Microsoft.AspNetCore.Mvc;
using Piranha;

namespace MyApp
{
public class CmsController : Controller
{
private readonly IApi _api;

public CmsController(IApi api)
{
_api = api;
}

//
// Default route for pages.
//
[Route("page")]
public IActionResult Page(Guid id, bool startpage = false)
{
...
}

 //
// Default route for posts.
//
[Route("post")]
public IActionResult Post(Guid id)
{
...
}

//
// Default route for archive pages.
//
[Route("archive")]
public IActionResult Archive(Guid id, int? year = null, int? month = null,
int? page = null, Guid? category = null, Guid? tag = null)
{
...
}
}
}

Mapping Content Types To Routes

In most cases you want to map a given content type to a certain route, so you know which model type to load. Here's an example where the page type StandardPage uses the default action, and the page type StartPage uses another action. Please note that you need to specify on your content types which route to use, there is no automatic convention where content types are sent to a route matching their name.

using Microsoft.AspNetCore.Mvc;
using Piranha;

namespace MyApp
{
public class CmsController : Controller
{
private readonly IApi _api;

public CmsController(IApi api)
{
_api = api;
}

//
// Default route for pages.
//
[Route("page")]
public IActionResult Page(Guid id)
{
return View(_api.GetById<StandardPage>(id));
}

//
// Custom route for the start page.
//
[Route("start")]
public IActionResult StartPage(Guid id, bool startpage = false)
{
return View(_api.GetById<StartPage>(id));
}

...
}
}

For detailed information on how to setup content types and configure their routes, please refer to How To Setup Content.