I have a custom View Engine that derives from WebFormViewEngine. There’s a lot of stuff going on in here, mostly caching. I want to be able to use WebFormViewEngine AND RazorViewEngine at the same time, is this possible? Ideally I’d like to do;
ViewEngines.Add(new MyViewEngine<WebFormsViewEngine>());
ViewEngines.Add(new MyViewEngine<RazorViewEngine>());
if a .ascx/.aspx/.master file exists then use WebForms, otherwise use Razor is a .cshtml file exists.
EDIT: I should of worded my question better. As my custom view engine derives from WebFormViewEngine it obviously uses WebForms, I can’t derive from two classes. I can derive from RazorViewEngine but then I’ll loose WebForms. I can duplicate my code entirely, derive from RazorViewEngine and edit the views file extensions, etc. but as I said I’ve got a lot of custom code in my view engine and would be duplicating hundreds of lines.
WebFormViewEngine and RazorViewEngine derive from BuildManagerViewEngine which in turn implements IViewEngine. The problem with that is I have to implement methods CreatePartialView() and CreateView() but how would I know what to return (WebForms/Razor?) using generics?
In the end I ended up with creating an abstract class:
public abstract class MyViewEngine : BuildManagerViewEngine, IViewEngine { }and then implement/override theFindViewandFindPartialViewmethods (which had my caching code in). I had an abstract methodpublic abstract void SetSearchPaths();that was called in myctor.I then created another class that derived from
MyViewEnginefor WebForms specifically:and did exactly the same for Razor but change the file extensions to .cshtml/.vbhtml and change
WebFormViewtoRazorView. Add them to theViewEnginecollection:and now they work perfectly again, side by side with all of my custom caching logic.