+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to add tags in head from MVC widget controller?

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

8 posts, 0 answered
  1. Marc
    Marc avatar
    3 posts
    Registered:
    14 Oct 2012
    28 Sep 2015
    Link to this post

    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!

     

     

     

     

  2. Stoyan Dimitrov
    Stoyan Dimitrov avatar
    12 posts
    Registered:
    02 Aug 2016
    29 Sep 2015
    Link to this post
    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
     
  3. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    08 Oct 2015 in reply to Stoyan Dimitrov
    Link to this post

    Try this:

    var page = HttpContext.Current.Handler as Page;

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

    etc...

     

  4. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    08 Oct 2015 in reply to Stephen2
    Link to this post

    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.

     

  5. Erik
    Erik avatar
    7 posts
    Registered:
    03 Aug 2015
    04 Nov 2015 in reply to Stoyan Dimitrov
    Link to this post

    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"

  6. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    07 Dec 2016
    06 Nov 2015
    Link to this post
    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
     
  7. Adam
    Adam avatar
    23 posts
    Registered:
    05 Apr 2013
    23 Aug in reply to Stephen2
    Link to this post

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

  8. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    23 Aug in reply to Adam
    Link to this post
    Nice, thanks for sharing exact code, knew it was something easy like that!
8 posts, 0 answered