Set ViewData outside the controller

Question!

I'm trying to create a logger that will write the queries LINQ executes to the page formatted with the great javascript library SyntaxHighlighter.

For that, I've set the DataContext's Log property to my custom logger, which is working well.

Now I just need to get the current Controller object (outside the controller execution context) so I can set some ViewData with what needs to be outputted.

Any suggestions?

By : changelog


Answers
If you want to perform operations outside of the controller, then action-filters (etc) are a good bet; just inherit from ActionFilterAttribute, override OnActionExecuting (etc) to inject data into the view, and mark your controller with [YourCustomFilter].

Like this


(original; I may have misunderstood)

It would be better to use dependency injection here, by passing the log-writer into the repository as a constructor argument (ideally via something like StructureMap, which works very well with MVC via StructureMapControllerFactory, or similar).



This video can help you solving your question :)
By: admin