WorkContext.CurrentSite.HomePage is always null

Topics: Core, General, Writing themes
Apr 17, 2014 at 7:47 PM
I've create a Filter that attempts to determine if the current page is the homepage by interrogating the WorkContext.CurrentSite.HomePage, but this property is always null.
    public class HomepageLayoutFilter : FilterProvider, IResultFilter
    {
        private readonly IWorkContextAccessor _wca;

        public HomepageLayoutFilter(IWorkContextAccessor wca)
        {
            _wca = wca;
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var workContext = _wca.GetContext();
            var routeValues = filterContext.RouteData.Values;

            workContext.Layout.IsHomePage = workContext.CurrentSite.HomePage.EndsWith(";" + routeValues["Id"]);
        }

        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }
    }
I have checked that the homepage Alias is set correctly. Why doesn't this property work properly?

I am running Orchard 1.8 but also tried it on earlier versions with the same result.
Developer
Apr 18, 2014 at 1:31 PM
I don't really know what that property should contain (I think it's a leftover from old times when routes were handled differently) but it definitely isn't an indicator whether you're on the home page right now.
You could play with fetching the route for ~/ and comparing the result with the current route values. Or the other way around, you could take the current route values and use UrlHelper to create a URL from them and check whether the result is "/" + AppPath.
Apr 19, 2014 at 3:00 PM
Thanks Piedone.

I did wonder if it was just old code. Makes me think that perhaps there should be an [Obsolete] attribute added to this property - but then I guess you can't do that with dynamic classes?

I will have a go at an alternative approach using your suggested approaches.

Thanks again.
Developer
Apr 19, 2014 at 3:03 PM
I agree but I'm not entirely sure about the purpose of that property, probably it's still needed. Would you mind opening an issue about it so it doesn't get forgotten?
Apr 19, 2014 at 3:14 PM
Developer
Apr 19, 2014 at 3:17 PM
Thanks!
Apr 19, 2014 at 3:47 PM
No probs.

So any clues as to how I might go about fetching the route for ~/ ?

I'm guessing you would use AliasMap.TryGetAlias() but how do I get access to AliasMap from within the ResultExecuting method of a filter?
Developer
Apr 19, 2014 at 4:04 PM
I think there is a service in Orchard.Alias for that or maybe it's possible through some standard MVC helper, am not sure and can't check now. But probably you could just check from the HttpContext whether the current path is "/". I can't remember but there is a property in HttpContext.Request.Url that should return "/" no matter what on the home page.
Apr 19, 2014 at 4:09 PM
Yes, of course I should be using IAliasService, this has a Lookup method, not having success yet but this looks like it might be the right track.

I'm keen to know how to get the homepage from any page/context so testing HttpContext.Request.Url isn't really an option.
Apr 19, 2014 at 4:18 PM
In case this helps anyone:

The trick is to use the IAliasService.Get() method with an empty string to get HomePage route.

IAliasService.Lookup() goes the other way round and looks up an alias when given a route.
Nov 26, 2014 at 12:10 AM
I was trying to determine if I was on the homepage from within my theme's Layout.cshtml file; what has been working so far is to check
if (WorkContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath == "~/") {
     //on the homepage
}
For other pages, this property equals "~/auto/route/slug-for-page". So I guess this method assumes your homepage alias/slug is the empty string "".

Note I couldn't check the WorkContext.HttpContext.Request.Path because this includes the virtual directory (/OrchardLocal/) setup as part of the default Orchard (full source) install, and it likely will be different when the theme is in production.