Check if page in episerver is opened from edit/admin mode

Check if page in episerver is opened from edit/admin mode

This code can be used to check if a page in episerver is opened in edit mode. It uses HttpContext.Current.Request.UrlReferrer.LocalPath to see if the word admin or edit exists in the url.

Check edit mode code example

private bool IsOpenedInEditMode()
{
    bool isInEditMode = false;
    If(HttpContext.Current.Request.UrlReferrer != null)
    {
        isInEditMode = Regex.IsMatch(HttpContext.Current.Request.UrlReferrer.LocalPath, "/admin/|/edit/", RegexOptions.IgnoreCase);
    }
    return isInEditMode;
}