Insert your view engine at correct position

D e v e l o p m e n t , E p i s e r v e r
Mattias Olsson 10/5/2016 7:12:00 PM

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);