Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 572
Charasala Avatar asked Nov 27 '11 09:11

Charasala


People also ask

What is view engine in MVC?

View Engine renders the view into html form to the browser. By default, Asp.net MVC comes with two flavors Form (ASPX) and Razor View Engine. There are many types of view engines. These engine works between your view and browser to provide HTML to your browser.

What is icomposite view engine in MVC?

Composite View Engine Microsoft. Asp Net Core. Mvc. View Engines. IComposite View Engine Finds the view with the given viewName using view locations and information from the context.

What are the different types of view engines?

Asp Net Core. Mvc. View Engines Defines the contract for a view engine. Microsoft. Asp Net Core. Mvc. Razor. IRazor View Engine Microsoft. Asp Net Core. Mvc. Razor. Razor View Engine Microsoft. Asp Net Core. Mvc. View Engines. Composite View Engine Microsoft.

How to create your own view engine?

When you create your own view engine, you have to just think about how you want to write your views. The easiest approach to create custom view engine is just derive a new view engine from abstract VirtualPathProviderViewEngine Class. This base class can take care of the all low-level mechanics of finding and caching views.


1 Answers

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.

like image 91
Marco Miltenburg Avatar answered Oct 06 '22 17:10

Marco Miltenburg