Esempio n. 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 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);
                    }

                    
                    if (deferredCq.IsDomCreated)
                    {
                        writer.Write(deferredCq.Dom.Render());
                        return;
                    }
                }
            }
            base.RenderView(viewContext, writer, instance);
        }
Esempio n. 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);
        }