How to add <meta> tags in head from MVC widget control

Posted by Community Admin on 04-Aug-2018 02:08

How to add tags in head from MVC widget controller?

All Replies

Posted by Community Admin on 28-Sep-2015 00:00

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!

 

 

 

 

Posted by Community Admin on 29-Sep-2015 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 08-Oct-2015 00:00

Try this:

var page = HttpContext.Current.Handler as Page;

page.Header.Controls.Add(new HtmlMeta() Name = "test", Content = "yay" );

etc...

 

Posted by Community Admin on 08-Oct-2015 00:00

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.

 

Posted by Community Admin on 04-Nov-2015 00:00

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"

Posted by Community Admin on 06-Nov-2015 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 23-Aug-2016 00:00

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!!";

Posted by Community Admin on 23-Aug-2016 00:00

Nice, thanks for sharing exact code, knew it was something easy like that!

This thread is closed