Application Service

Last Updated: 2018-10-09

The ApplicationService is a scoped service that simplifies access to common objects for the current request. 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.

The Application Service is intended to be used in web applications and is a part of the package Piranha.AspNetCore.

@inject Piranha.AspNetCore.Services.IApplicationService WebApp

Global Helpers

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.

Api

@WebApp.Api

Gives you access to the current scoped IApi instance.

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 provider.

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

@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.

GetContent

@WebApp.Site.GetContent<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.