5

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.

Falkayn
  • 559
  • 3
  • 15

3 Answers3

5

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.

Falkayn
  • 559
  • 3
  • 15
3

It's too easy, here's what I did:

  1. Create a class derived from VirtualPathProviderViewEngine (in fact, you can just copy WebFormsViewEngine and edit it slightly).

  2. Create a class derived from IView. There's only one method - put your magic here.

  3. In Global.asax.cs:

    ViewEngines.Engines.Add( new MyEngine() );

Community
  • 1
  • 1
Dan
  • 3,179
  • 1
  • 16
  • 13
2

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)

ajma
  • 12,106
  • 12
  • 71
  • 90