How to add tags in head from MVC widget controller?
Hi,
I have an MVC widget on a page in hybrid mode using Sitefinity 7.0.
The widget needs to dynamically update the <meta name="description" content="..."/> tag in the head as well as insert new meta tags, specifically open graph tags for sharing.
The <head> is coming from the .master template that is part of the theme.
How can the MVC widget update it in its controller action?
Cheers!
Hello Marc,
Thank you for contacting us.
You can check the following article which is closely related to your question.
http://www.thingswelearned.com/article/show/adding-updating-meta-tags-dynamically-in-asp-net-mvc-5-asp-net
In your scenario to update dynamically the html head tag you can use only jQuery because the ViewBag and the RenderSection from MVC are not relevant in pages with hybrid mode.
Hope you find this information useful.
Regards,
Stoyan Dimitrov
Telerik
Try this:
var page = HttpContext.Current.Handler as Page;
page.Header.Controls.Add(new HtmlMeta() Name = "test", Content = "yay" );
etc...
You may have to get current context like:
SystemManager.CurrentHttpContext
Can't remember...
But when coding in Sitefinity MVC, just remember it's not traditional MVC, so sometimes the straight up MVC tutorials aren't 100% accurate for Sitefinity MVC coding.
That URL is a total red herring. . . And jQuery is totally useless if you are going for SEO since the meta tags need be rendered server-side not client-side. At least you have confirmed what I learned the hard way -- i.e. you can't do normal MVC things inside feather MVC widgets. . .
If you're looking for the short answer it's:
"You can't"
Hi Erik,
The answer has been provided in that discussion:
http://www.sitefinity.com/developer-network/forums/suggestions---/seo-fields-for-news-and-blog-items
Regards,
Svetoslav Manchev
Telerik
This code worked for me:
var page = (Page) SystemManager.CurrentHttpContext.CurrentHandler;
page.Header.Controls.Add(new HtmlMeta Name = "og:title", Content = "Test OG!");
page.Header.Description = "override!!";
page.Header.Title = "override!!";
Nice, thanks for sharing exact code, knew it was something easy like that!