Remove LogIn link

Topics: Controls
May 8, 2015 at 9:50 PM
Edited May 8, 2015 at 9:51 PM
I want remove the Login link. I tried disable to menu item from "..\BlogEngine.NET\AppCode\Controls\PageMenu.cs" file but it didn't work. I'm using the latest version(3.1). How can I solve this?

Here is the PageMenu.cs content:
Image
May 9, 2015 at 2:22 PM
AppCode/Controls/PageMenu.cs provides a convenient way to label menu items, however it does seem reasonable that if you omit the label then you omit the item.
The simplest and best thing would be to alter BlogEngine.Core/Web/Controls/PageMenu.cs but then you would have to remember to change the core code any time you upgrade.

Alternatively, you could just copy the code from the core into AppCode/Controls/PageMenu.cs and modify.
The following slightly modified version simply checks the page name in the AddMenuItem method and if it's not given will not add the item.
// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Builds nested page list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace App_Code.Controls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using BlogEngine.Core;

    /// <summary>
    /// Summary description for PageMenu
    /// </summary>
    public class PageMenu : Control
    {
        public override void RenderControl(HtmlTextWriter writer)
        {
            var menu = new PageMenu();

            menu.Home = Resources.labels.home;
            menu.Contact = Resources.labels.contact;
            menu.Archive = Resources.labels.archive;
            //menu.Logon = Resources.labels.login;            
            menu.Logoff = Resources.labels.logoff;
            writer.Write(menu.Html);
            writer.Write(Environment.NewLine);
        }

        #region Properties

        private bool _ulIdSet = false;
        string _curPage = HttpUtility.UrlEncode(GetPageName(HttpContext.Current.Request.RawUrl.ToLower()));

        /// <summary>
        /// Home label.
        /// </summary>
        public string Home { get; set; }

        /// <summary>
        /// Contact label.
        /// </summary>
        public string Contact { get; set; }

        /// <summary>
        /// Archive label.
        /// </summary>
        public string Archive { get; set; }

        /// <summary>
        /// Logon label.
        /// </summary>
        public string Logon { get; set; }

        /// <summary>
        /// Logoff label.
        /// </summary>
        public string Logoff { get; set; }

        /// <summary>
        /// The HTML for the control to display on the page.
        /// </summary>
        public string Html
        {
            get
            {
                HtmlGenericControl ul = BindPages();
                System.IO.StringWriter sw = new System.IO.StringWriter();
                ul.RenderControl(new HtmlTextWriter(sw));
                return sw.ToString();
            }
        }

        #endregion

        private HtmlGenericControl BindPages()
        {
            // recursivly get all children of the root page
            HtmlGenericControl ul = GetChildren(Guid.Empty);

            // items that will be appended to the end of menu list
            AddMenuItem(ul, Contact, Utils.RelativeWebRoot + "contact.aspx");

            if (Security.IsAuthenticated)
            {
                AddMenuItem(ul, Logoff, Utils.RelativeWebRoot + "Account/login.aspx?logoff");
            }
            else
            {
                AddMenuItem(ul, Logon, Utils.RelativeWebRoot + "Account/login.aspx");
            }

            return ul;
        }

        bool HasChildren(Guid pageId)
        {
            bool returnValue = false;

            foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
            {
                if (page.ShowInList && page.IsPublished)
                {
                    if (page.Parent == pageId)
                    {
                        returnValue = true;
                        break;
                    }
                }
            }

            return returnValue;
        }

        HtmlGenericControl GetChildren(Guid parentId)
        {
            HtmlGenericControl ul = new HtmlGenericControl("ul");

            if (!_ulIdSet)
            {
                ul.Attributes.Add("id", "menu-topmenu");
                ul.Attributes.Add("class", "menu");
                _ulIdSet = true;

                AddMenuItem(ul, Home, Utils.RelativeWebRoot + "default.aspx");
                AddMenuItem(ul, Archive, Utils.RelativeWebRoot + "archive.aspx");
            }

            foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
            {
                if (page.ShowInList && page.IsPublished)
                {
                    if (page.Parent == parentId)
                    {
                        HtmlGenericControl li = new HtmlGenericControl("li");
                        string pageName = HttpUtility.UrlEncode(GetPageName(page.RelativeLink.ToString().ToLower()));

                        HtmlAnchor anc = new HtmlAnchor();
                        anc.HRef = page.RelativeLink.ToString();
                        anc.InnerHtml = page.Title;
                        anc.Title = page.Description;

                        if (pageName == _curPage)
                        {
                            anc.Attributes.Add("class", "current");
                        }

                        li.Controls.Add(anc);

                        if (HasChildren(page.Id))
                        {
                            HtmlGenericControl subUl = GetChildren(page.Id);
                            li.Controls.Add(subUl);
                        }
                        ul.Controls.Add(li);
                    }
                }
            }

            return ul;
        }

        private void AddMenuItem(HtmlGenericControl ul, string pageName, string pageUrl)
        {
            if (!string.IsNullOrEmpty(pageName))
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlAnchor anc = new HtmlAnchor();

                anc.HRef = pageUrl;
                anc.InnerHtml = pageName;
                anc.Title = pageName;

                if (GetPageName(pageUrl).ToLower() == _curPage.ToLower())
                {
                    anc.Attributes.Add("class", "current");
                }

                li.Controls.Add(anc);
                ul.Controls.Add(li);
            }
        }

        static string GetPageName(string requestPath)
        {
            if (requestPath.IndexOf('?') != -1)
                requestPath = requestPath.Substring(0, requestPath.IndexOf('?'));
            return requestPath.Remove(0, requestPath.LastIndexOf("/") + 1).ToLower();
        }
    

    }
}
Of course, you will still need to remember to keep a copy of this control and use it any time you upgrade