Exemple #1
0
        /// <summary>
        /// Intercept rendering and create a CsQuery object
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="writer"></param>
        /// <param name="instance"></param>
        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            if (viewContext.Controller is ICsQueryController)
            {
                CsQueryController controller = (CsQueryController)viewContext.Controller;

                bool hasLayout  = _LayoutController != null;
                bool hasMethods = HasCqMethods(viewContext);

                if (hasLayout || hasMethods)
                {
                    var sb = new StringBuilder();
                    var sw = new StringWriter(sb);

                    // render the view into out local writer
                    base.RenderView(viewContext, sw, instance);
                    var cqDoc = CQ.Create(sb.ToString());

                    if (hasLayout)
                    {
                        LayoutController.Doc = cqDoc;
                        MethodInfo mi;
                        if (CqMethods.TryGetValue("layout_Cq_Start", out mi))
                        {
                            mi.Invoke(LayoutController, null);
                        }
                    }

                    if (hasMethods)
                    {
                        string currentAction  = GetCurrentAction(viewContext);
                        string controllerName = GetControllerName(viewContext);

                        if (IsPartial)
                        {
                            // viewName is the name of the cshtml file with underscores e.g "ASP.namespace_subspace_shared__layout_cshtml"
                            // we've mapped things that could be partials with the "_cshtml" attached to the key in the dictionary

                            var    viewInfo     = ParseInstance(instance);
                            string prefix       = controllerName + "_Cq_";
                            string actionPrefix = prefix + currentAction + "_";

                            MethodInfo mi;

                            if (CqMethods.TryGetValue(prefix + viewInfo.FileName, out mi) ||
                                CqMethods.TryGetValue(prefix + viewInfo.FilePath, out mi))
                            {
                                mi.Invoke(controller, new object[] { cqDoc });
                            }

                            // look for an action-specific method
                            if (CqMethods.TryGetValue(actionPrefix + viewInfo.FileName, out mi) ||
                                CqMethods.TryGetValue(actionPrefix + viewInfo.FilePath, out mi))
                            {
                                mi.Invoke(controller, new object[] { cqDoc });
                            }
                        }
                        else
                        {
                            controller.Doc = cqDoc;
                            MethodInfo method;

                            if (CqMethods.TryGetValue(controllerName + "_Cq_Start", out method))
                            {
                                method.Invoke(controller, null);
                            }


                            if (CqMethods.TryGetValue(controllerName + "_Cq_" + currentAction, out method))
                            {
                                method.Invoke(controller, null);
                            }

                            if (CqMethods.TryGetValue(controllerName + "_Cq_End", out method))
                            {
                                method.Invoke(controller, null);
                            }
                        }
                    }

                    if (hasLayout)
                    {
                        MethodInfo mi;
                        if (CqMethods.TryGetValue("layout_Cq_End", out mi))
                        {
                            mi.Invoke(LayoutController, null);
                        }
                    }


                    if (Options.HasFlag(ViewEngineOptions.EnableScriptManager))
                    {
                        ManageScripts(cqDoc, (System.Web.Mvc.WebViewPage)instance);
                    }

                    writer.Write(cqDoc.Render());
                    return;
                }
            }
            base.RenderView(viewContext, writer, instance);
        }
Exemple #2
0
        /// <summary>
        /// Intercept rendering and create a CsQuery object
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="writer"></param>
        /// <param name="instance"></param>
        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            if (viewContext.Controller is ICsQueryController)
            {
                CsQueryController controller = (CsQueryController)viewContext.Controller;

                bool hasMethods          = HasCqMethods(viewContext);
                bool scriptManagerActive = !IsPartial && Options.HasFlag(ViewEngineOptions.EnableScriptManager);

                if (hasMethods || scriptManagerActive)
                {
                    var deferredCq = new DeferredCq(base.RenderView, viewContext, instance);

                    if (hasMethods)
                    {
                        string currentAction  = GetCurrentAction(viewContext);
                        string controllerName = GetControllerName(viewContext);

                        if (IsPartial)
                        {
                            // viewName is the name of the cshtml file with underscores e.g "ASP.namespace_subspace_shared__layout_cshtml"
                            // we've mapped things that could be partials with the "_cshtml" attached to the key in the dictionary

                            var    viewInfo     = ParseInstance(instance);
                            string prefix       = controllerName + "_Cq_";
                            string actionPrefix = prefix + currentAction + "_";

                            MethodInfo mi;

                            if (CqMethods.TryGetValue(prefix + viewInfo.FileName, out mi) ||
                                CqMethods.TryGetValue(prefix + viewInfo.FilePath, out mi))
                            {
                                mi.Invoke(controller, new object[] { deferredCq.Dom });
                            }

                            // look for an action-specific method
                            if (CqMethods.TryGetValue(actionPrefix + viewInfo.FileName, out mi) ||
                                CqMethods.TryGetValue(actionPrefix + viewInfo.FilePath, out mi))
                            {
                                mi.Invoke(controller, new object[] { deferredCq.Dom });
                            }
                        }
                        else
                        {
                            controller.Deferred = deferredCq;
                            MethodInfo method;

                            if (CqMethods.TryGetValue(controllerName + "_Cq_Start", out method))
                            {
                                method.Invoke(controller, null);
                            }


                            if (CqMethods.TryGetValue(controllerName + "_Cq_" + currentAction, out method))
                            {
                                method.Invoke(controller, null);
                            }

                            if (CqMethods.TryGetValue(controllerName + "_Cq_End", out method))
                            {
                                method.Invoke(controller, null);
                            }
                        }
                    }
                    if (scriptManagerActive)
                    {
                        ManageScripts(deferredCq.Dom, (System.Web.Mvc.WebViewPage)instance, viewContext);
                    }


                    if (deferredCq.IsDomCreated)
                    {
                        writer.Write(deferredCq.Dom.Render());
                        return;
                    }
                }
            }
            base.RenderView(viewContext, writer, instance);
        }