How does MVC3 choose which ViewEngine to use if I have several engines in the ViewEngines collection?

I have a built-in viewer. In the same project, I would like to use Razor for some pages and my own engine for some pages. How does MVC choose which engine to use? BTW, my user engine does not require any templates; it displays pages based on metadata from the database. For my custom mechanism, I do not want to configure template files. I expect that there should be a way to use the framework to use a specific engine based on the name of the controller and the name of the action. Is this flexibility in MVC3?

+6
source share
2 answers

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

It is possible for several viewing mechanisms to work side by side. If you do not want your view mechanism to be used in a specific situation, you return new ViewEngineResult(new string[0]); from FindView or FindPartialView , and MVC will choose a different viewing engine. If you want your view mechanism to be used, you return a valid ViewEngineResult indicating the view class (which implements IView ) that you want to render .

There are some features with the useCache parameter. If you want to know more, there was a wonderful presentation about creating your own viewing engine in TechEd 2011 from Louis DeJardin. You can find the Video Writing ASP.NET MVC View Engine in Channel9.

+6
source

I think the easiest way is to implement 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 the default IViewPageActivator by default. You enter it in DependencyResolver , http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html .

This might be easier to use if you are using a dependency injection infrastructure like NInject or Unity.

0
source

Source: https://habr.com/ru/post/902479/


All Articles