How does MVC3 picks which ViewEngine to use if I have multiple engines in ViewEngines collection?
Asked Answered
K

2

7

I have a custom view engine developed internally. In the same project, I would like to use Razor for some pages and my custom engine for some pages. How does MVC framework choose which engine to use? BTW, my custom engine does not require any templates, it renders pages based on meta-data from database. For my custom engine I don't want to setup any template files. What I am expecting is there should be a way to drive framework to use certain engine based on the controller name and action name. Is this flexibility exists in MVC3?

Kaylil answered 27/11, 2011 at 9:49 Comment(0)
Y
6

Your view engine should implement the IViewEngine interface. After you registered your view engine with the ViewEngines.Engines.Add() method, the MVC framework will call FindView and FindPartialView whenever it needs a view engine to render a view.

It's absolutely possible for multiple view engines to operate side by side. If you don't want your view engine to be used in a specific situation you return new ViewEngineResult(new string[0]); from FindView or FindPartialView and MVC will choose another view engine. If you do want your view engine to be used you return a valid ViewEngineResult pointing to the view class (that is implementing IView) that you want to Render the result.

There are some specifics with the useCache parameter. If you want to know more, there was an excellent presentation on building your own view engine at TechEd 2011 by Louis DeJardin. You can find the video of Writing an ASP.NET MVC View Engine at Channel9.

Yuji answered 27/11, 2011 at 10:30 Comment(1)
Thanks Marco. " If you don't want your view engine to be used in a specific situation you return new ViewEngineResult(new string[0]); from FindView or FindPartialView and MVC will choose another view engine.". This indeed worked well. But its unnecessary trip to my custom engine when I Exactly know which Engine too use in some cases. Probably MVC designers didn't think of this situation. Thanks again!Kaylil
H
0

I think the easiest way would be to implement a IViewPageActivator, http://bradwilson.typepad.com/blog/2010/10/service-location-pt11-view-page-activator.html and http://msdn.microsoft.com/en-us/library/system.web.mvc.iviewpageactivator(v=vs.98).aspx.

I think that returning null from the Create method will make it later default to the default IViewPageActivator. You inject it in the DependencyResolver, http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html.

It might be easier to use if you are using a dependency injection framework as NInject or Unity.

Humphreys answered 27/11, 2011 at 9:56 Comment(2)
Yes. I am using dependency injection with Unity. Can you please explain how it can be resolved implementing IViewPageActivator. If add any custom implementation to IViewPageActivator then again the same problem I posted exists here too. Can I have more than one IViewPageActivator? How to configure to one or the other?Kaylil
I don't know if you can have more than one IViewPageActivator, but it shouldn't be hard to implement your custom one that solves that. Also, I think returning null will cause the default one to kick in.Humphreys

© 2022 - 2024 — McMap. All rights reserved.