Integrating with old releases

From NDjango
Jump to: navigation, search

ASP.MVC ( and older)

To integrate NDjango as a view engine for ASP.MVC, you'll need to add a reference to the NDjango.ASPMVCIntegration assembly, and make a small change to your Global.asax.cs:

  • Add a protected member called (well.. anything really, but use this for consistency) "ndjangoHandle":

<source lang="csharp"> public class MvcApplication : System.Web.HttpApplication { protected NDjango.ASPMVCIntegration.NDjangoHandle ndjangoHandle;

... } </source>

  • Add a no-parameter constructor, with the following:

<source lang="csharp"> public MvcApplication() { ndjangoHandle = new NDjangoHandle(this); } </source>

... aand that's it. This will register the NDjango ASPMVC View Engine, and allow you to both infer view paths from controllers (standard behavior), and specify explict template paths via ViewResult View(string viewName); instead of ViewResult View();. ".django" extension must be ommited.

Implementation note
To track the official django implementation, session values are exposed to the django context unqualified. That means that if your ViewData has a value named foo and so does your session, the NDjango integration will throw a runtime exception notifying you of the collision. This is a construct that ASP.MVC allows, and NDjango does not.
Implementation note
The asp.mvc-specific format of the {% url %} tag is as follows: {% url action_expr [controller_expr] [as name] %}, where action_expr is an expression (string literal or variable) that returns a valid action, and controller_expr is an optional expression that returns a controller name. These values are then fed into UrlHelper.Action(action[, controller]), e.g. - {% url "index" "home" as v %} {{ v }}, which will yield /virtualDir/home/index