6

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
  • 1
    Storing the templates in the database is the easy part. Rendering a strongly typed ASP.NET MVC view page to a string inside a controller action could be more challenging. Sep 23, 2009 at 20:15
  • 2
    I've ended up using NVelocity view engine (nvelocity.codeplex.com) for the exact same purpose. Sep 23, 2009 at 20:17

4 Answers 4

2

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.

3
  • 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. Sep 23, 2009 at 21:57
  • 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... Sep 24, 2009 at 4:41
  • I render Views saved in the database using virtual file providers and a custom viewEngine from the Datalayer using Razor. Razor can run independently but obviously the implementation is strict. ie- no ViewBag
    – Piotr Kula
    Jul 29, 2014 at 9:54
2

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.

1

Phil Haack has a great article on something similar here.

I think it could be adapted to your needs.

Kindness,

Dan

0

You have to create your viewengine and so your buildmanager. Your viewengine can simply retreive the aspx part from a db and invoke the webform engine. Not easy but doable, it mostly depends on how bad you need this and how will it rewards you with a nice ROI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.