Skip to content
Shannon Deminick edited this page Apr 27, 2015 · 2 revisions

MVC specific docs

Make sure that you have seen the Quick Start guide as it explains the basic concepts of registering dependencies using MVC.

Setup

In order for CDF to work with MVC you must have the HttpModule registered in your web.config (See Configuration for details). However, if you are using the Razor view engine you can configure your application to use the CDF view engine instead of relying on the HttpModule.

Setup for Razor view engine

The preferred approach if using the Razor view engine is to rely on the MVC view engine to process the page output instead of the HttpModule. Please note however that if you require Rogue File Processing than the HttpModule must remain registered.

To setup CDF for use with the razor view engine we need to replace the currently registered default Razor view engine with the CDF view engine. This can be done in your global.asax:

using ClientDependency.Core.Mvc;

public class MyApplication : System.Web.HttpApplication
{
	protected void Application_Start()
    {
		//an extension method to replace the default razor view engine with 
		//the CDF view engine.
		ViewEngines.Engines.ReplaceDefaultRazorEngine(new CdfRazorViewEngine());
	}
}

Once you've set that up you can remove the HttpModule registration from your web.config (as long as you don't need Rogue File Detection)

Dynamic/Runtime dependency registration

You can get access to the current DependencyRenderer if one exists by using this code:

var renderer = DependencyRenderer.GetInstance(currentHttpContext);

If one doesn't exist, you can create one for the current request dynamically using:

bool successfullyCreated;
var loader = DependencyRenderer.TryCreate(this.Page, out successfullyCreated);

Accessing the DependencyRenderer at runtime in your c# code means that you can register dependencies dynamically, even from within your controller. This could be based on certain runtime criteria of the current executing action, current member, or anything you want.

There are many overloaded methods of the DependencyRenderer.RegisterDependency to register dependencies dynamically at runtime.

Clone this wiki locally