API Reference: AllowViewAttribute Class

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

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

Usage

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

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

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

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

Example

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

[Page]
[AllowView("HomePage")]
public class HomePage: IContentType
{
    // ...
}

[Page]
[AllowView(ViewPath = "~/Views/ContentPage.cshtml")]
public class ContentPage: IContentType
{
    // ...
}