views:

237

answers:

3

What I'm trying to do is generate email content using different templates.

I think ASP.NET MVC could be a good fit for this. My controller would get the necessary data for the email and decide which view (template) to render. (It's more that just a simple mail-merge, there would need to be conditional blocks, foreach's, etc.)

I want to be able to store these templates in a database rather than as files in the web application, so new templates can be easily added from the web application itself.

Is this possible? I would prefer to be able to use the WebForms view engine, but would consider other view engines if that's not possible. I would ideally like to use typed views.

+2  A: 

Unfortunately, the WebForms ViewEngine uses some internal classes to compile the aspx and ascx files, so this is not possible. That ViewEngine requires that the Views are available as files in a folder inside the site's root (virtual folder will also work IIRC).

There are other ViewEngines that may fit your purpose better, including some that use XSLT.

AFAIR it is possible to mix several ViewEngines in the same application, so you could use the WebForms engine for your normal web pages, and a different one for your emails.

Mark Seemann
So does the WebForms ViewEngine require the views to already be compiled in the application? Or does it compile them at run time? What I'm getting at is if it would maybe be possible through reflection to access these internal classes and compile a view from content that I provide at runtime. I haven't yet looked at the inner workings of the view engine myself.
sectrean
It compiles the views at run-time. Even if you could use reflection to compile them at run-time it's not going to be trivial, as much of it is hard-wired to compile from files... It may be possible though...
Mark Seemann
+2  A: 

You can indeed use multiple view engines in the same application. The framework will ask each of the engines whether it is capable of rendering the requested view. Check MvcContrib for other available view engines ...

As stated in the previous answer, the WebForms ViewEngine makes the assumption that the views (aspx, ascx) are stored physically on the file system, just like ASP.NET does (although in ASP.NET you have something like the VirtualPathProvider) that was - I believe - originally added to the framework to support e.g. SharePoint).

If you want to create your own view engine, you should implement IViewEngine, and add the view engine to the ViewEngines collection e.g. in Application_Start.

sergevm
+1  A: 

Phil Haack has a great article on something similar here.

I think it could be adapted to your needs.

Kindness,

Dan

Daniel Elliott