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 fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post

How to send Slack messages programmatically using C#

In this post I show you how you can send Slack messages programmatically using C#

Read Post

How to fix the SVG 404 error when using Umbraco and Azure Blob Storage

In this post I show you how to solve the SVG 404 error when working with Umbraco and Azure Blob Stor…

Read Post