About this post

This post shows you how you can add the meta tags which are required for search engines and sharing on facebook and twitter.

Watch the video

You can follow along with the video for instructions on what to do in the umbraco back end and for more explanation about the code.

Model

Create a model to hold the information

namespace FionaWhitfieldArt.Models
{
    public class MetaDataModel
    {
        public string Url { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Keywords { get; set; }
        public string ImageUrl { get; set; }
    }
}

View

Add this RenderAction call in head of the main View, in my case it is called WebBase.cshtml

<head>
    @{ Html.RenderAction("RenderMetaData", "SiteLayout"); }
</head>

Partial View

Create a partial view to render the model data

@model FionaWhitfieldArt.Models.MetaDataModel
<title>@Model.Title</title>
<meta name="description" content="@Model.Description" /> <meta name="keywords" content="@Model.Keywords" /> <meta name="author" content="Fiona Whitfield" />
<!-- Facebook and Twitter integration --> <meta property="og:title" content="@Model.Title" /> <meta property="og:image" content="@Model.ImageUrl" /> <meta property="og:url" content="@Model.Url" /> <meta property="og:site_name" content="Fiona Whitfield Art" /> <meta property="og:description" content="@Model.Description" /> <meta name="twitter:title" content="@Model.Title" /> <meta name="twitter:image" content="@Model.ImageUrl" /> <meta name="twitter:url" content="@Model.Url" /> <meta name="twitter:card" content="summary_large_image" />
<link rel="canonical" href="@Model.Url" >

Controller

Create a method in the Controller

using Umbraco.Web.Mvc;
using System.Web.Mvc;
using FionaWhitfieldArt.Models;
using Umbraco.Core.Models;
using Umbraco.Web;
namespace FionaWhitfieldArt.Controllers {     public class SiteLayoutController : SurfaceController     {         private string PartialViewPath(string name)         {             return $"~/Views/Partials/SiteLayout/{name}.cshtml";         }
        public ActionResult RenderMetaData()         {             MetaDataModel model = new MetaDataModel();             IPublishedContent homePage = CurrentPage.AncestorOrSelf("home");             string domainAddress = homePage.UrlWithDomain();             string title = CurrentPage.GetPropertyValue<string>("title");             model.Title = !string.IsNullOrEmpty(title) ? title : CurrentPage.Name;             model.Description = CurrentPage.HasProperty("description") ? CurrentPage.GetPropertyValue<string>("description") : null;             model.Keywords = CurrentPage.HasProperty("keywords") ? CurrentPage.GetPropertyValue<string>("keywords") : null;             if(CurrentPage.HasProperty("socialShareImage"))             {                 int mediaId = CurrentPage.GetPropertyValue<int>("socialShareImage");                 var mediaItem = Umbraco.Media(mediaId);                 model.ImageUrl = $"{domainAddress.TrimEnd('/')}{mediaItem.Url}";             }                      model.Url = CurrentPage.UrlWithDomain();             return PartialView(PartialViewPath("_MetaData"), model);         }
    } }

I hope this has helped you, let me know in the comments if you need any further help.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to ignore a file locally in git without ignoring it from the repository

This post shows you how you can ignore a file in git locally without ignoring it from the repository…

Read Post

How to get the picked item name in Stacked Content and Nested Content using ncNodeName

This post shows you how you can get the name of the picked item in Nested Content or Stacked Content…

Read Post

How I use source control for my Umbraco website source code and media

In this post I share my approach to using source control for my Umbraco website source code and medi…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post