What's the correct way of defining a <meta> tag inside <html><head>?

  1. Avatar of Gabrielius

    Hello,

    What's the correct way of defining a <meta> tag inside <html><head>? Currently I am doing a work-around by defining the <meta> tag inside @section Styles.

    Thx

  2. Avatar of Mantas

    Hi,

    You can add meta keywords and meta description in 'Edit SEO' window.

    If you need more customization you can attach to the page rendering event and modify meta data there:

    BetterCms.Events.RootEvents.Instance.PageRendering += Instance_PageRender;

        private void Instance_PageRender(PageRenderingEventArgs args)
        {
            if (args.RenderPageData.Metadata == null)
            {
                args.RenderPageData.Metadata = new List<IPageActionProjection>();
            }
            args.RenderPageData.Metadata.Add(new MetaDataProjection("custom name", "custom content"));
        }
    
  3. Avatar of Gabrielius

    Thanks for fast response. Actually, I need to include tag:

    <meta charset="utf-8" />

    Which makes MetaDataProjection unusable (I tried it just after I posted the question). Any other solutions?

  4. Avatar of Mantas

    Well, I got the right result with

        public class CustomMetaProjection : BetterCms.Core.Modules.Projections.HtmlElementProjection
        {
            private Dictionary<string, string> KeyValuePairs;
    
            public CustomMetaProjection(string key, string value) : base("meta", true)
            {
                KeyValuePairs = new Dictionary<string, string> {{key, value}};
            }
            protected override void OnPreRender(HtmlControlRenderer controlRenderer, BetterCms.Core.DataContracts.IPage page, HtmlHelper html)
            {
                foreach (var keyValuePair in KeyValuePairs)
                {
                    controlRenderer.Attributes[keyValuePair.Key] = keyValuePair.Value;
                }
            }
        }
    

    and then

        private void Instance_PageRender(PageRenderingEventArgs args)
        {
            if (args.RenderPageData.Metadata == null)
            {
                args.RenderPageData.Metadata = new List<IPageActionProjection>();
            }
            args.RenderPageData.Metadata.Add(new CustomMetaProjection("charset", "utf-8"));
        }
    

    although I agree that there's too much overhead for such a simple task

    In general, placing the line in Styles section is absolutely fine and it has been done this way in many other projects

  5. Avatar of Gabrielius

    Thanks for showing how to use HtmlElementProjection, it worked.

    I just added my CustomMetaDataProjection in the root layout, worked like a charm:

    @{ Layout = "~/Areas/bcms-Root/Views/Shared/BaseLayout.cshtml"; Model.Metadata.Add(new CustomMetaProjection("charset", "utf-8")); }

* Mandatory
* Mandatory
* Mandatory

Verify that you are human