At my current project Bugfighter there is some Javascript- code, which is needed for all pages. The main part of this code is configuration – which projects are there, which projects can the user see, what is the URL for the delete action, …

The problem is that this code can not be included statically, as it has to query the database or build links based on the current page (using the ASP.NET MVC Routing engine).

Using sub- controllers

The problem can be solved easily using SubControllers. This feature, included in the MvcContrib project allows invoking any controller action in any view. I am basically creating this sub- controller first in my common base controller and invoking the action in the shared master page:

ViewData[ViewDataKeys.CommonJavascript] = IoC.Resolve<UtilController>().GetResult(this);
<% ViewData.Get<Action>(ViewDataKeys.CommonJavascript).Invoke(); %>

On the server side a action with the name of the subcontroller gets invoked. This action can do anything as any other action – in my case I am just rendering a shared view which outputs the settings to the response stream.



No Responses Yet to “Handling common dynamic content for every web- page”  

  1. No Comments Yet

Leave a Reply