Meny NO

Insert your view engine at correct position

The other day I had to deal with a mysterious error with the Episerver Shell search function. You know the little search icon to the right in the dashboard drop-down menu. When I clicked it nothing happened. I looked at the request in developer toolbar, discovered the exception and this is how I fixed it.

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);
kommentarer drevet av Disqus