Custom Home Page Provider for Static Pages in Orchard

#orchard

Posted by admin on September 01, 2019

Orchard is built with a home page provider pattern so that you can say this content item is the home page. The problem is if your homepage is something that doesn't exist as a content item.. i.e. a static page.

The problem is that there is no way to tell orchard that a custom static page is the home page, so you need to buy in to the provider model, and the ‘trick’ it. So here is how I have done it.

public class CustomHomePageProvider : IHomePageProvider {
    public CustomHomePageProvider() {
        CurrentSite.HomePage = "CustomHomePageProvider;0";
    }


<span class="kwrd">protected</span> <span class="kwrd">virtual</span> ISite CurrentSite { get; [UsedImplicitly] <span class="kwrd">private</span> set; }

<span class="kwrd">public</span> <span class="kwrd">string</span> GetProviderName() {
    <span class="kwrd">return</span> <span class="str">&quot;CustomHomePageProvider&quot;</span>;
}

<span class="kwrd">public</span> ActionResult GetHomePage(<span class="kwrd">int</span> itemId) {
    <span class="rem">//  We ignore the itemId as this is the part that we dont want.</span>
    <span class="kwrd">return</span> <span class="kwrd">new</span> ViewResult {
        ViewName = <span class="str">&quot;~/Modules/Orchard.LathamImages/Views/Test/Index.ascx&quot;</span>,
        ViewData = <span class="kwrd">new</span> ViewDataDictionary&lt;IndexViewModel&gt;(<span class="kwrd">new</span> IndexViewModel())
    };
}
}

First you need to create a class that implements the IHomePageProvider.

You can then make the assignment of the home page provider by always reassigning it in your ctor. (Yes I know I'm calling a virtual method in a ctor)

Next, set the view name in the GetHomePage method passing in any know ViewModel etc.

That's It… now every time you start your site up, you will be using this home page.

The problem is that you can now never change the homepage… Well, food for thought I guess..