Thursday, 2 October 2014

How to Get the Current Page in a Block Controller or Action Filter in Optimizely CMS

If you need to get the current routed page inside a block controller, view component, or action filter in Optimizely CMS, the usual approach is to use IPageRouteHelper.

If you want the current routed content more generically, use IContentRouteHelper.

Modern Approach

In newer Optimizely CMS projects, it is better to use constructor injection instead of ServiceLocator.Current.

Get the Current Page

using EPiServer.Web.Routing;

public class MyService
{
    private readonly IPageRouteHelper _pageRouteHelper;

    public MyService(IPageRouteHelper pageRouteHelper)
    {
        _pageRouteHelper = pageRouteHelper;
    }

    public PageData GetCurrentPage()
    {
        return _pageRouteHelper.Page;
    }
}

Get the Current Page Reference

using EPiServer.Web.Routing;

public class MyService
{
    private readonly IPageRouteHelper _pageRouteHelper;

    public MyService(IPageRouteHelper pageRouteHelper)
    {
        _pageRouteHelper = pageRouteHelper;
    }

    public PageReference GetCurrentPageReference()
    {
        return _pageRouteHelper.PageLink;
    }
}

Get the Current Routed Content

If you do not specifically need a page, use IContentRouteHelper instead:

using EPiServer.Web.Routing;

public class MyService
{
    private readonly IContentRouteHelper _contentRouteHelper;

    public MyService(IContentRouteHelper contentRouteHelper)
    {
        _contentRouteHelper = contentRouteHelper;
    }

    public IContent GetCurrentContent()
    {
        return _contentRouteHelper.Content;
    }

    public ContentReference GetCurrentContentReference()
    {
        return _contentRouteHelper.ContentLink;
    }
}

Using It in an Action Filter

If you need the current page inside an action filter, you can resolve the helper from the request services:

using EPiServer.Web.Routing;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var pageRouteHelper = context.HttpContext.RequestServices.GetRequiredService<IPageRouteHelper>();
        var currentPage = pageRouteHelper.Page;
        var currentPageReference = pageRouteHelper.PageLink;

        base.OnActionExecuting(context);
    }
}

Legacy Example

If you are working with older code, you may still see ServiceLocator.Current being used:

using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;

var pageRouteHelper = ServiceLocator.Current.GetInstance<IPageRouteHelper>();
var currentPage = pageRouteHelper.Page;
var pageReference = pageRouteHelper.PageLink;

Important Note

Page and PageLink are only available when the current request is actually routed to a page. In some non-page contexts, such as certain preview scenarios, they may be null. If you need a more general routed object, IContentRouteHelper is the safer choice.

Final Thought

If your code only needs the routed content, prefer IContentRouteHelper. If your code specifically depends on a page, use IPageRouteHelper.

About the Author

Adnan Zameer, Principal Developer at Optimizely UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizely CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.

0 comments :

Post a Comment