Please note that version 9 is still in beta. For documentation on the latest stable release, please change to version 8.
Contribute to Docs Contribute to Docs

Application Service

The Application Service is part of the package Piranha.AspNetCore and is intended to be used when building integrated web applications with ASP.NET.

The Application Service is a scoped service that simplifies access to common objects for the current request. It can be used both from your .cshtml views or pages, or from your backend Controllers. In the following examples we assume that the Application Service has been injected with the name WebApp with the following line in your _ViewImports.cshtml.

@inject Piranha.AspNetCore.Services.IApplicationService WebApp

Registering The Service

The Application Service is needed by all middleware components in Piranha CMS and should be added in ConfigureServices by calling the extension method:

services.AddPiranhaApplication();

Properties

Api
@WebApp.Api

Gives you access to the current scoped IApi instance.

PageId
@WebApp.PageId

Gets the id of the currently requested page. This is useful when rendering menus or other structures based on the sitemap.

Url
@WebApp.Url

Gets the currently requested URL before it was rewritten by the Piranha middleware.

Helpers

MediaHelper

The MediaHelper exposes helpers for accessing and manipulating uploaded media. Please note that for these methods to work you need to have registered an Image Manipulation service.

ResizeImage
@WebApp.Media.ResizeImage(ImageField image, int width, int? height = null)

Resizes the given ImageField to the specified dimensions and returns the PublicUrl to the resized file. If only width is specified the image is scaled with the same proportions. If both width and height is specified the image is both scaled and cropped.

@WebApp.Media.ResizeImage(Media image, int width, int? height = null)

Resizes the given Media to the specified dimensions and returns the PublicUrl to the resized file. If only width is specified the image is scaled with the same proportions. If both width and height is specified the image is both scaled and cropped.

SiteHelper

The SiteHelper provides information regarding the active site for the current request.

Sitemap
@{
    var sitemap = WebApp.Site.Sitemap;
}

Gets the Sitemap for the current site. This can for example be used for rendering menus. The result only contains the currently published nodes, but hidden nodes are included and must be handled by the application.

GetContentAsync
@{
    var content = await WebApp.Site.GetContentAsync<T>();
}

Gets the current site content with the specified Site Type. This is used if you have registered and selected a Type for your site for Site Global Content.