/// <summary> /// Adds the before delegate to the Before pipeline if the module is not currently executing, /// or executes the delegate directly and returns any response returned if it is. /// Uses <see cref="RouteExecutionEarlyExitException"/> /// </summary> /// <param name="module">Current module</param> /// <param name="beforeDelegate">Delegate to add or execute</param> /// <param name="earlyExitReason">Optional reason for the early exit (if necessary)</param> public static void AddBeforeHookOrExecute(this INancyModule module, Func <NancyContext, Response> beforeDelegate, string earlyExitReason = null) { if (module.RouteExecuting()) { var result = beforeDelegate.Invoke(module.Context); if (result != null) { throw new RouteExecutionEarlyExitException(result, earlyExitReason); } } else { module.Before.AddItemToEndOfPipeline(beforeDelegate); } }