More in this section

Forums / Developing with Sitefinity / Prevent Search from indexing content inside an MVC widget

Prevent Search from indexing content inside an MVC widget

4 posts, 0 answered
  1. Chip
    Chip avatar
    14 posts
    Registered:
    25 Nov 2013
    24 Jul 2015
    Link to this post

    I have a custom MVC widget that is included on most every page on our site. Whenever someone uses a search term that is in the content of this MVC widget, the search results returns every single page. How do I hide the content of the MVC widget from search?

     Please don't point me to the blog post about the layout template. I don't use any layout templates on my MVC widgets. I just use the simple MVC View to render the content. 

  2. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    06 Sep 2017
    28 Jul 2015
    Link to this post
    Hello Chip,

    You can try to set an attribute on the Actions that you do not want to be indexed:
    using System;
    using System.Linq;
    using System.Web.Mvc;
       
    namespace SitefinityWebApp.Attributes
    {
        public class ExcludeFromSearchAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (IsSearchMode(filterContext))
                    filterContext.Result = new EmptyResult();
                else
                    base.OnActionExecuting(filterContext);
            }
       
            private bool IsSearchMode(ActionExecutingContext filterContext)
            {
                if (filterContext.HttpContext.CurrentHandler == null)
                    return true;
      
                var page = filterContext.HttpContext.CurrentHandler as System.Web.UI.Page;
                if (page != null && page.Items["IsInIndexMode"] == null)
                    return false;
      
                var mvcHandler = filterContext.HttpContext.CurrentHandler as System.Web.Mvc.MvcHandler;
                if (mvcHandler != null)
                    return false;
     
                 return true;
            }     
        }
    }

    Then here is how it looks like on one of the controller actions:
    [ExcludeFromSearchAttribute]
    public ActionResult Index()
    {
         //...
    }

    You need also to register it on Application_Start in the global.asax
    GlobalFilters.Filters.Add(new ExcludeFromSearchAttribute());

    I hope this helps.

    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
     
  3. Dan
    Dan avatar
    2 posts
    Registered:
    01 Aug 2014
    28 Apr
    Link to this post

    If you add the ExcludeFromSearchAttribute object to the GlobalFilters.Filters list then it will run for every MVC widget in the system, including the default MVC widgets like the content block, preventing them from being included in the search index.

    I don't think that is what the original poster was looking for.

  4. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    29 Apr in reply to Dan
    Link to this post

    Another good option  for author is to use this attribute on the controller, which he wants to hide:

    [IndexRenderMode(IndexRenderModes.NoOutput)]

    It's working for Sitefinity 7.3 and later

4 posts, 0 answered