Allow Layout attribute

API Reference: AllowLayoutAttribute Class

The AllowLayoutAttribute can be applied to page models. The attribute specifies a layout that may be used for pages with this model. Layouts are represented by (Razor) views in your application.

Usage

Decorate the page model class with the AllowLayoutAttribute. The layout can be specified either by name or by view path.

Layout name The layout name is the filename of the (Razor) view without extension and path. So for a view with path ~/Views/ContentPage.cshtml, the layout name is ContentPage. If you want to specify the layout by name, pass the layout name in the attribute constructor. The name is case insensitive. If the name conflicts with other layouts with the same name but a different view path, provide the full view path with the ViewPath property instead.

Layout view path The layout view path is the absolute path of the (Razor) view from the content root, e.g. ~/Views/ContentPage.cshtml. If you want to specify the layout by view path, pass the layout view path as a named property in the attribute constructor. The view path is case insensitive.

The attribute can be applied multiple times on the class. The layouts from all applied attributes are added to the allowed layout list. The attribute is not inherited, so any allowed layouts that you have specified on a base class of this model will have to be specified explicitly on derived model classes.

Default Convention

If AllowLayoutAttribute is not specified on a page model, there is no restriction on allowed layouts, and pages of this model can have any layout that exists in the system.

Example

[AllowLayout("HomePage")]
public class HomePage: IPageModel
{
    // ...
}

[AllowLayout(ViewPath = "~/Views/ContentPage.cshtml")]
public class ContentPage: IPageModel
{
    // ...
}

The first example selects a layout by name, and the second example selects a layout by view path.