Prevent Search from indexing content inside an MVC widget
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.
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
;
[ExcludeFromSearchAttribute]
public
ActionResult Index()
//...
GlobalFilters.Filters.Add(
new
ExcludeFromSearchAttribute());
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.
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