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.

Watch on YouTube

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.

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.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.