protected
void
Page_Load(
object
sender, EventArgs e)
HtmlMeta tag =
new
HtmlMeta();
tag.Attributes.Add(
"property"
,
"og:title"
);
tag.Content = Page.Title;
Page.Header.Controls.Add(tag);
John, this is the right answer. See my solution -- MetaDescription is a custom field we created off of the BlogPost Content Model.
public class MetaDataAttribute : ActionFilterAttribute private string metaDescription; public override void OnActionExecuting(ActionExecutingContext filterContext) var actionParameters = filterContext.ActionParameters; if (actionParameters.ContainsKey("item")) var item = actionParameters["item"] as Content; if (item != null) this.metaDescription = item.GetValue<string>("MetaDescription"); public override void OnActionExecuted(ActionExecutedContext filterContext) filterContext.Controller.ViewBag.Description = this.metaDescription;
Erik,
Thank you for taking the time to reply, but I'm not sure if I'm not explaining something properly or if I'm missing something. Setting filterContext.Controller.ViewBag.Description = "whatever", adds the following meta tag to the head:
<meta name="description" content="whatever" /> I'm not trying to add a description meta tag, I'm trying to do add a custom (opengraph) meta tag, this: <meta property="og:title" content="The Rock" /> Setting the Title, Keywords, and Description on the ViewBag does not help me at all... Can you think of any way to add the custom meta tag?
John, you should be able to access any ViewBag parameters from your MVC _Layout -- e.g.
filterContext.Controller.ViewBag.ogTitle= "The Rock"
<meta property="og:title" content="@ViewBag.ogTitle" />
I have tried that, but the ViewBag.ogTitle is empty when the layout is rendered. What am I missing?
Hmmm, good question. . . must be a Sitefinityism -- I've had some success submitting issues to the feather developers on github:
github.com/.../feather
Is adding a custom meta tag still not possible in feather?
It's hard to believe every sitefinity feather website is missing this functionality..
using
System.Collections.Generic;
System.Web;
System.Web.Mvc;
System.Web.UI;
System.Web.UI.HtmlControls;
Telerik.Sitefinity.Frontend.News.Mvc.Controllers;
Telerik.Sitefinity.News.Model;
Telerik.Sitefinity.Services;
Telerik.Sitefinity.Web.Events;
namespace
SitefinityWebApp
public
class
FacebookMetaAttribute : ActionFilterAttribute
string
OgTitle
get
;
set
override
OnActionExecuting(ActionExecutingContext filterContext)
base
.OnActionExecuting(filterContext);
if
(filterContext.Controller.GetType() ==
typeof
(NewsController) && filterContext.ActionDescriptor.ActionName ==
"Details"
)
var actionParameters = filterContext.ActionParameters
as
IDictionary<
>;
var newsItem = actionParameters[
"newsItem"
]
NewsItem;
(newsItem !=
null
this
.OgTitle = newsItem.Title;
.Register();
private
Register()
EventHub.Unsubscribe<IPagePreRenderCompleteEvent>(OnPagePreRenderCompleteEventHandler);
EventHub.Subscribe<IPagePreRenderCompleteEvent>(OnPagePreRenderCompleteEventHandler);
OnPagePreRenderCompleteEventHandler(IPagePreRenderCompleteEvent @
event
(!
.IsNullOrEmpty(
.OgTitle))
tag.Content =
.OgTitle;
@
.Page.Header.Controls.Add(tag);
Application_Start(
Bootstrapper.Initialized +=
.Bootstrapper_Initialized;
Bootstrapper_Initialized(
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
(e.CommandName ==
"Bootstrapped"
GlobalFilters.Filters.Add(
FacebookMetaAttribute());
Thank you Velizar!
This works like a charm!
I already have natural meta data fields in my Custom Modules. I don't want to create a TitleTag, such that a user has to enter the same text as they have entered into another field say Title? And yes the module is running and already has data in it, so I couldn't change the field names I understand if I wanted to - which I don't. Why doesn't the Widget simply accept ANY field from a custom module, so I can set MetaTitleField to "Title" for instance and MetaDescriptionField to "Quote", etc.
Update: doh it does didn't set PageTitleMode to Replace
Can someone suggest how I might be able to employ these solutions for the MVC widgets that are automatically generated when a dynamic content type is created within a custom module. How can I change the meta title and meta description when I am using one of these widgets?
Any help would be appreciated!
Thanks