public void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); var controllerName = context.ControllerName; if (string.IsNullOrEmpty(ViewName)) { ViewName = context.ActionToRender; } var searchedLocations = new List<string>(); var factories = ViewEngines.Engines.OfType<SparkViewFactory>(); if (!factories.Any()) throw new CompilerException("No SparkViewFactory instances are registered"); foreach (var factory in factories) { var descriptor = factory.DescriptorBuilder.BuildDescriptor( new BuildDescriptorParams("", controllerName, ViewName, MasterName, false, factory.DescriptorBuilder.GetExtraParameters(context)), searchedLocations); descriptor.Language = LanguageType.Javascript; var entry = factory.Engine.CreateEntry(descriptor); context.RequestContext.Response.ContentType.Value = "text/javascript"; context.RequestContext.Response.Write(entry.SourceCode); return; } throw new CompilerException("Unable to find templates at " + string.Join(", ", searchedLocations.ToArray())); }
public virtual IDictionary<string, object> GetExtraParameters(ControllerContext controllerContext) { var extra = new Dictionary<string, object>(); foreach (var filter in Filters) filter.ExtraParameters(controllerContext, extra); return extra; }
/// <summary> /// Render a view. /// </summary> /// <param name="writer"></param> /// <param name="context"></param> /// <param name="viewData"></param> public void Render(TextWriter writer, ControllerContext context, IViewData viewData) { foreach (IViewEngine engine in _viewEngines.Values) { engine.Render(context, viewData, writer); writer.Flush(); } }
/// <summary> /// Determines if this controller director can process the specified context. /// </summary> /// <param name="context">Context being processed.</param> /// <returns><c>true</c> if this director should process the context; otherwise <c>false</c>.</returns> public bool CanProcess(ControllerContext context) { if (!context.Uri.AbsolutePath.StartsWith(_uri)) return false; if (context.UriSegments.Length < _uriSegments) return false; // using default action if (context.UriSegments.Length == _uriSegments) { if (_defaultMethod == null) return false; } else { string actionName = context.UriSegments[_uriSegments]; MethodInfo method = GetMethod(context.RequestContext.Request.Method, actionName); if (method == null) return false; } return true; }
void IViewEngine.ReleaseView(ControllerContext controllerContext, IView view) { ReleaseView(controllerContext, view); }
public abstract void ExtraParameters(ControllerContext context, IDictionary<string, object> extra);
ViewEngineResult IViewEngine.FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) { return FindPartialView(controllerContext, partialViewName, useCache); }
ViewEngineResult IViewEngine.FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { return FindView(controllerContext, viewName, masterName, useCache); }
//private readonly ViewEngineResult _cacheMissResult = new ViewEngineResult(new string[0]); private ViewEngineResult FindViewInternal(ControllerContext controllerContext, string viewName, string masterName, bool findDefaultMaster, bool useCache) { var searchedLocations = new List<string>(); var targetNamespace = controllerContext.Controller.GetType().Namespace; var controllerName = controllerContext.RouteData.GetRequiredString("controller"); var descriptorParams = new BuildDescriptorParams( targetNamespace, controllerName, viewName, masterName, findDefaultMaster, DescriptorBuilder.GetExtraParameters(controllerContext)); ISparkViewEntry entry; if (useCache) { if (TryGetCacheValue(descriptorParams, out entry) && entry.IsCurrent()) { return BuildResult(controllerContext.RequestContext, entry); } return _cacheMissResult; } var descriptor = DescriptorBuilder.BuildDescriptor( descriptorParams, searchedLocations); if (descriptor == null) return new ViewEngineResult(searchedLocations); entry = Engine.CreateEntry(descriptor); SetCacheValue(descriptorParams, entry); return BuildResult(controllerContext.RequestContext, entry); }
public SparkViewDescriptor CreateDescriptor( ControllerContext controllerContext, string viewName, string masterName, bool findDefaultMaster, ICollection<string> searchedLocations) { var targetNamespace = controllerContext.Controller.GetType().Namespace; var controllerName = controllerContext.RouteData.GetRequiredString("controller"); return DescriptorBuilder.BuildDescriptor( new BuildDescriptorParams( targetNamespace, controllerName, viewName, masterName, findDefaultMaster, DescriptorBuilder.GetExtraParameters(controllerContext)), searchedLocations); }
public virtual ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) { return FindViewInternal(controllerContext, partialViewName, null /*masterName*/, false, useCache); }
public virtual void ReleaseView(ControllerContext controllerContext, IView view) { Engine.ReleaseInstance((ISparkView)view); }
/// <summary> /// Create a view. /// </summary> /// <param name="context">Context to render</param> public void Render(ControllerContext context, IViewData viewData, TextWriter writer) { _spark. throw new NotImplementedException(); }
public virtual ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { return FindViewInternal(controllerContext, viewName, masterName, true, useCache); }
/// <summary> /// Process a request. /// </summary> /// <param name="context">Request information</param> /// <returns>What to do next.</returns> public ProcessingResult Process(RequestContext context) { _current = this; var routingContext = new RoutingContext(context.Request); var routeResult = RoutingService.Route(routingContext); if (routeResult == null) return ProcessingResult.Continue; var controllerContext = new ControllerContext(context) {ActionName = routeResult.ActionName, ControllerUri = routeResult.ControllerUri}; var result = _factory.Invoke(routeResult.ControllerType, routeResult.Action, controllerContext); var viewData = result as IViewData; if (viewData != null) { _logger.Trace("Rendering action " + controllerContext.ActionName); RenderView(controllerContext, viewData); } else { var action = result as IActionResult; if (action != null) { ProcessingResult processingResult = _actionProvider.Invoke(context, action); if (processingResult == ProcessingResult.Abort) return processingResult; } } return ProcessingResult.SendResponse; }
private void RenderView(ControllerContext controllerContext, IViewData viewData) { //ViewProvider.Get(controllerContext.ViewPath + ".*"); // do not dispose writer, since it will close the stream. TextWriter bodyWriter = new StreamWriter(controllerContext.RequestContext.Response.Body); _viewEngines.Render(bodyWriter, controllerContext, viewData); bodyWriter.Flush(); }
/// <summary> /// Process a request. /// </summary> /// <param name="context">Request information</param> /// <returns>What to do next.</returns> public ProcessingResult Process(RequestContext context) { _current = this; var controllerContext = new ControllerContext(context); foreach (ControllerDirector director in _directors) { if (!director.CanProcess(controllerContext)) continue; _logger.Debug("Controller '" + director.Uri + "' is handling the request."); Controller controller = null; try { object result = director.Process(controllerContext, out controller); if (result == null) continue; var viewData = result as IViewData; if (viewData != null) { _logger.Trace("Rendering action " + controllerContext.ActionName); RenderView(controllerContext, viewData); } else { var action = result as IActionResult; if (action != null) { ProcessingResult processingResult = _actionProvider.Invoke(context, action); if (processingResult == ProcessingResult.Abort) return processingResult; } } return ProcessingResult.SendResponse; } finally { // We want to keep the controller during the whole // rendering process. To be able to access the controller from // the view. if (controller != null) director.Enqueue(controller); } } return ProcessingResult.Continue; }
public override void ExtraParameters(ControllerContext context, IDictionary<string, object> extra) { var theme = Convert.ToString(_selector(context)); if (!string.IsNullOrEmpty(theme)) extra["theme"] = theme; }
/// <summary> /// Implemented by custom descriptor builder to quickly extract additional parameters needed /// to locate templates, like the theme or language in effect for the request /// </summary> /// <param name="controllerContext">Context information for the current request</param> /// <returns>An in-order array of values which are meaningful to BuildDescriptor on the same implementation class</returns> public IDictionary<string, object> GetExtraParameters(ControllerContext controllerContext) { return null; }