How do you write your own View engine for ASP.NET MVC?
-
23-08-2019 - |
Question
I know that ASP.NET MVC will allow me to swap in various View engines that other people have created, but I am wondering how can I create my own View engine?
More info: We have our own webforms based CMS and the main selling point about MVC is that it gives us cleaner HTML (which our designers would love). However we have a desire to create our own domain-specific View implementation.
Solution
OK, I did some Google-fu and found these posts:
Adding support for skins in an ASP.NET MVC application
Partial Rendering & View Engines in ASP.NET MVC
I think I can work it out from what these guys say - I just hope that it still applies in the latest ASP.NET MVC drop!
EDIT: There is also a nice post about using VB.NET XML literals for a view engine.
OTHER TIPS
It's too easy, here's what I did:
- Create a class derived from VirtualPathProviderViewEngine (in fact, you can just copy WebFormsViewEngine and edit it slightly).
- Create a class derived from IView. There's only one method - put your magic here.
In Global.asax.cs:
ViewEngines.Engines.Add( new MyEngine() );
Check out http://mvccontrib.org/ for a couple samples of different ViewEngines. There are 4 included (well, minus one that's obsolete but the code is still available)