dwebb
posted this
22 August 2016
@using A4DN.Core.MVC.CMS.Models
@{
var title = HttpContext.Current.Items["Meta.Title"] as string ?? "Surround Technologies LLC";
var description = HttpContext.Current.Items["Meta.Description"] as string;
var defaultTwitterImageURL = HttpContext.Current.Items["Meta.ModuleImageURL"] as string;
if (defaultTwitterImageURL != null && defaultTwitterImageURL.StartsWith("/"))
{
defaultTwitterImageURL = Request.Url.GetLeftPart(UriPartial.Authority) + defaultTwitterImageURL;
}
var defaultFacebookImageURL = HttpContext.Current.Items["Meta.ModuleImageURL"] as string;
if (defaultFacebookImageURL != null && defaultFacebookImageURL.StartsWith("/"))
{
defaultFacebookImageURL = Request.Url.GetLeftPart(UriPartial.Authority) + defaultFacebookImageURL;
}
var openGraphType = HttpContext.Current.Items["Meta.OGType"] as string ?? "website";
var additionalScriptModels = HttpContext.Current.Items["Meta.ScriptModels"] as List<AB_PageScriptInfoModel>;
var metaModel = new AB_PageMetaInfoModel();
metaModel.Add(new AB_PageMetaEntity { Name = "viewport", Content = "width=device-width, initial-scale=1.0" });
metaModel.AddRange(new List<AB_PageMetaEntity>
{
new AB_PageMetaEntity { Name = "description", Content = description },
new AB_PageMetaEntity { Name = "keywords", Content = HttpContext.Current.Items["Meta.Keywords"] as string },
new AB_PageMetaEntity { Name = "author", Content = "Surround Technologies, LLC" },
new AB_PageMetaEntity { Name = "generator", Content = "Accelerator Development Solutions - http://www.surroundtech.com/accelerator" },
new AB_PageMetaEntity { Name = "rating", Content = "General" },
new AB_PageMetaEntity { Name = "robots", Content = "Index, Follow" },
new AB_PageMetaEntity { Name = "revisit-after", Content = "7 days" },
});
metaModel.AddRange(new List<AB_PageMetaEntity>
{
new AB_PageMetaEntity { Name = "twitter:card", Content = "summary_large_image" },
new AB_PageMetaEntity { Name = "twitter:site", Content = "@SurroundTech" },
new AB_PageMetaEntity { Name = "twitter:title", Content = title },
new AB_PageMetaEntity { Name = "twitter:description", Content = description },
new AB_PageMetaEntity { Name = "twitter:image", Content = defaultTwitterImageURL },
});
metaModel.AddRange(new List<AB_PageMetaEntity>
{
new AB_PageMetaEntity { Property = "og:title", Content = title },
new AB_PageMetaEntity { Property = "og:description", Content = description },
new AB_PageMetaEntity { Property = "og:type", Content = openGraphType },
new AB_PageMetaEntity { Property = "og:url", Content = Request.Url.AbsoluteUri },
new AB_PageMetaEntity { Property = "og:image", Content = defaultFacebookImageURL },
new AB_PageMetaEntity { Property = "og:site_name", Content = "Surround Technologies, LLC" }
});
metaModel.Add(new AB_PageMetaEntity { Name = "apple-mobile-web-app-capable", Content = "yes" });
}
@Html.DisplayFor(m => metaModel)
The final rendering is done by calling DisplayFor() on the AB_PageMetaInfoModel object. You need ~/Views/Shared/DisplayTemplates/AB_PageMetaInfoModel.cshtml:
@model A4DN.Core.MVC.CMS.Models.AB_PageMetaInfoModel
@foreach (var entity in Model.Where(m => !String.IsNullOrWhiteSpace(m.Content)))
{
if (!String.IsNullOrWhiteSpace(entity.Name))
{
<meta name="@entity.Name" content="@entity.Content">
}
else if (!String.IsNullOrWhiteSpace(entity.Property))
{
<meta name="@entity.Property" content="@entity.Content">
}
}