Prevent Search from indexing content inside an MVC widget

Posted by Community Admin on 04-Aug-2018 03:19

Prevent Search from indexing content inside an MVC widget

All Replies

Posted by Community Admin on 24-Jul-2015 00:00

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. 

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

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
 

Posted by Community Admin on 28-Apr-2017 00:00

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.

Posted by Community Admin on 29-Apr-2017 00:00

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

This thread is closed