+1-888-365-2779
Try Now
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

2 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:
    29 Nov 2016
    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
     
2 posts, 0 answered