The Application Service is part of the package
Piranha.AspNetCore
and is intended to be used when building integrated web applications withASP.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
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();
...
});
@WebApp.Api
Gives you access to the current scoped IApi
instance.
@WebApp.PageId
Gets the id of the currently requested page. This is useful when rendering menus or other structures based on the sitemap.
@WebApp.CurrentPage
Gets the currently requested page if the current HTTP request is for a page.
@WebApp.CurrentPost
Gets the currently requested post if the current HTTP request is for a post.
@WebApp.Media
Gets the media helper which provides methods for manipulating media. You can read more about this in Media Helper.
@WebApp.Request
Gets the request helper which provides information about the current request. You can read more about this in Request Helper.
@WebApp.Site
Gets the site helper which provices information about the site currently being requested. You can read more about this in Site Helper.
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.
@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.
The
SiteHelper
provides information regarding the active site for the current request.
@{
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.
@{
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.