0

What is the best way to apply a treatment after the page is rendered (view calculated, html helpers calculated, partial views calculated and so on...) but before the response is sent to the client ? In order to modify this response (with DB access needed)...

Thank you

Proviste
  • 177
  • 13

2 Answers2

3

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onresultexecuted.aspx

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • FYI, this is called *before* a view is rendered. – Dave Aug 31 '11 at 15:12
  • @Dave: No. It's called ***after*** the view have been rendered. You might be thinking on OnActionExecuted. – jgauffin Aug 31 '11 at 15:27
  • Are you certain? I've tested this first hand (mvc-2) and, at least in MVC2, it's called before :( – Dave Aug 31 '11 at 15:29
  • Yes I'm sure. And the behavior haven't changed since mvc2. You can read the `ViewResultBase` source code here: http://aspnet.codeplex.com/SourceControl/changeset/view/68345#288022 – jgauffin Aug 31 '11 at 17:41
  • :( I'll have to revisit this then, because I'd wanted to teardown some Contextual stuff in the OnResultExecuted, which (definitely) causes a nullException when I access the object I'm looking to teardown from the view at render time... Thanks for the link. – Dave Sep 01 '11 at 07:33
  • OnResultExecuted is called for all action results. You need to check that the result contains a view. – jgauffin Sep 01 '11 at 09:02
  • Yep - turns out, I had some mini controller actions, which were doing the teardown - paying some attention to the ChilAction attribute's sorted it. Thanks for the pointer! – Dave Sep 01 '11 at 09:35
2

You want to create an HttpModule to handle post-processing events

Neil Fenwick
  • 6,106
  • 3
  • 31
  • 38