NuGet package | Owin.Framework.Pages.Html |
GitHub source | OwinFramework.Pages.Html |
Page elements handle requests matching the path and methods configured then return responses by rendering a tree-like heirachy of layouts, regions and components.
For simple pages it is often possible to define the page using a bare C# class with no implementation that is decorated with attributes. This is illustrated by the following example:
[IsPage("home")] [PartOf("usecase5")] [DeployedAs("usecase5")] [Route("/", Method.Get)] [UsesLayout("home")] internal class HomePage { } [IsLayout("home", "test1,test2")] [PartOf("usecase5")] [DeployedAs("usecase5")] [ZoneTemplate("test1", "/test1")] [ZoneTemplate("test2", "/test2")] internal class HomeLayout { }
In this use case the [IsPage()] attribute has properties that specify how Http requests are routed to the page.
The other attributes that you can attach to the page class are:
For simple pages the attribute based approach works just fine, but for real-wprld websites with dynamic pages and SEO needs, it is very likely that you will want to make your page classes inherit from the build in Page. The example below sets some properties in the constructor and overrides the writing of the page head to include some meta tags.
[UsesLayout("order_page_layout")] internal class OrderPage : Page { public MasterPage(IPageDependenciesFactory dependencies) : base(dependencies) { TitleFunc = rc => "Order number ABC123"; BodyClassNames = "my-app_order-page"; BodyId = Guid.NewGuid().ToShortString(); } public override IWriteResult WriteHeadArea(IRenderContext context) { context.Html.WriteUnclosedElement( "meta", "name", "description", "content", "This is the order page"); context.Html.WriteLine(); context.Html.WriteUnclosedElement( "meta", "name", "keywords", "content", "sample,demo"); context.Html.WriteLine(); context.Html.WriteUnclosedElement( "meta", "name", "author", "content", "Martin Halliday"); context.Html.WriteLine(); context.Html.WriteUnclosedElement( "meta", "name", "viewport", "content", "width=device-width, initial-scale=1.0"); context.Html.WriteLine(); return base.WriteHeadArea(context); } }
The following example defines a package that builds a page using the fluent builder.
public class CmsManagerPackage: IPackage { private readonly ITemplateBuilder _templateBuilder; private readonly INameManager _nameManager; public string NamespaceName { get; set; } public IModule Module { get; set; } public ElementType ElementType { get { return ElementType.Package; } } public string Name { get; set; } public CmsManagerPackage( IHostingEnvironment hostingEnvironment, ITemplateBuilder templateBuilder, INameManager nameManager) { _templateBuilder = templateBuilder; _nameManager = nameManager; Name = "cms_manager"; NamespaceName = "cmsmanager"; } IPackage IPackage.Build(IFluentBuilder fluentBuilder) { // -- snip-- // Code deleted from here to reduce complexity and focus on the page building aspect. // This example will not compile because of the missing piece. // -- snip-- var managerLayout = fluentBuilder.BuildUpLayout() .Name("manager") .ZoneNesting("editor,tools") .Region("editor", editorRegion) .Region("tools", toolsRegion) .Build(); fluentBuilder.BuildUpPage() .Name("cmsManager") .Route("/cms/manager", 0, Method.Get) .Layout(managerLayout) .Build(); return this; } }