I want to manipulate HTML using a custom view engine like this:
protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
{
if (viewContext.Controller is MyController)
{
.. alter the html then write to writer
} else {
base.RenderView(viewContext, writer, instance);
}
}
RenderView gets called for the view and each partial view, but I can’t figure out a reliable way to determine whether it’s rendering the outer view or a partial view.
The type of the writer object is HttpWriter for a partial view and StringWriter for the outer view. This seems like a bad way to test it, but so far it’s the only concrete thing I can find
The instance tells me the view or partial view name, but checking a string to see if I’m rendering a partial view or not seems brittle.
Is there any more direct way to determine if it’s a partial view or not?
You could pass a boolean parameter to your custom view from your custom view engine: