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 is registered automatically when calling UseCms in ConfigureServices.

services.AddPiranha(options =>
{
    options.UseCms();

    ...
});

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.

CurrentPage
@WebApp.CurrentPage

Gets the currently requested page if the current HTTP request is for a page.

CurrentPost
@WebApp.CurrentPost

Gets the currently requested post if the current HTTP request is for a post.

Helpers

Media

@WebApp.Media

Gets the media helper which provides methods for manipulating media. You can read more about this in Media Helper.

Request

@WebApp.Request

Gets the request helper which provides information about the current request. You can read more about this in Request Helper.

Site

@WebApp.Site

Gets the site helper which provices information about the site currently being requested. You can read more about this in Site Helper.

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.