Insert your view engine at correct position

Mattias Olsson 2016-10-05 23:25:38

The exception was: 

[InvalidOperationException: The model item passed into the dictionary is of type 'X', but this dictionary requires a model item of type 'Y'.]

Turned out we have a view in a folder named Search in our project and that view was selected by our custom view engine when doing a Shell search. Shell search is routed to /UIUrl/cms/Search/.

The solution? Either rename the controller and folder to something else or make sure our engine is inserted after EPiServer.Shell.Web.Mvc.ModuleViewEngineCollection. For obvious reasons, I went for the second option.

var moduleViewEngine = ViewEngines.Engines.FirstOrDefault(x => x.GetType() == typeof(EPiServer.Shell.Web.Mvc.ModuleViewEngineCollection));
var insertIndex = moduleViewEngine != null ? ViewEngines.Engines.IndexOf(moduleViewEngine) + 1 : 0;
ViewEngines.Engines.Insert(insertIndex, myCustomViewEngine);