Is it possible with ASP.NET MVC to render a view from a template pulled from a database?
Asked Answered
N

4

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.

Norvil answered 23/9, 2009 at 20:10 Comment(2)
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.Bony
I've ended up using NVelocity view engine (nvelocity.codeplex.com) for the exact same purpose.Bony
T
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.

Terminator answered 23/9, 2009 at 20:52 Comment(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.Norvil
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...Terminator
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 ViewBagExterior
K
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.

Kenrick answered 23/9, 2009 at 21:44 Comment(0)
P
1

Phil Haack has a great article on something similar here.

I think it could be adapted to your needs.

Kindness,

Dan

Pridemore answered 24/9, 2009 at 12:10 Comment(0)
F
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.

Fluffy answered 8/11, 2010 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.