Exemple #1
0
        public virtual void RenderView(ViewContext viewContext)
        {
            ViewContext = viewContext;
            InitHelpers();

            bool         createdSwitchWriter = false;
            SwitchWriter switchWriter        = viewContext.HttpContext.Response.Output as SwitchWriter;

            try
            {
                if (switchWriter == null)
                {
                    switchWriter        = new SwitchWriter();
                    createdSwitchWriter = true;
                }

                using (switchWriter.Scope(viewContext.Writer))
                {
                    if (createdSwitchWriter)
                    {
                        // It's safe to reset the _nextId within a Server.Execute() since it pushes a new TraceContext onto
                        // the stack, so there won't be an ID conflict.
                        int originalNextId = _nextId;
                        try
                        {
                            _nextId = 0;
                            viewContext.HttpContext.Server.Execute(
                                HttpHandlerUtil.WrapForServerExecute(this),
                                switchWriter,
                                true /* preserveForm */
                                );
                        }
                        finally
                        {
                            // Restore the original _nextId in case this isn't actually the outermost view, since resetting
                            // the _nextId may now cause trace ID conflicts in the outer view.
                            _nextId = originalNextId;
                        }
                    }
                    else
                    {
                        ProcessRequest(HttpContext.Current);
                    }
                }
            }
            finally
            {
                if (createdSwitchWriter)
                {
                    switchWriter.Dispose();
                }
            }
        }
Exemple #2
0
        public void SwitchWriter()
        {
            SwitchTemplate template     = new SwitchTemplate(Code.Local("value"));
            CaseTemplate   caseTemplate = new CaseTemplate(Code.String("one"));

            caseTemplate.Code.AddLine(Code.Comment("Some code here"));
            template.Cases.Add(caseTemplate);
            template.Default.AddLine(Code.Comment("Some code here"));
            SwitchWriter writer = new SwitchWriter();

            writer.Write(template, this.output);
            Assert.AreEqual("switch (value)\r\n{\r\n    case \"one\":\r\n        // Some code here\r\n        break;\r\n    default:\r\n        // Some code here\r\n        break;\r\n}", this.output.ToString());
        }
Exemple #3
0
 public WriterScope(SwitchWriter switchWriter, TextWriter writerToRestore)
 {
     _switchWriter = switchWriter;
     _writerToRestore = writerToRestore;
 }
Exemple #4
0
        public virtual void RenderView(ViewContext viewContext)
        {
            ViewContext = viewContext;
            InitHelpers();

            bool needServerExecute = false;

            SwitchWriter switchWriter = viewContext.HttpContext.Response.Output as SwitchWriter;
            if (switchWriter == null) {
                switchWriter = new SwitchWriter();
                needServerExecute = true;
            }

            using (switchWriter.Scope(viewContext.Writer)) {
                if (needServerExecute) {
                    // It's safe to reset the _nextId within a Server.Execute() since it pushes a new TraceContext onto
                    // the stack, so there won't be an ID conflict.
                    int originalNextId = _nextId;
                    try {
                        _nextId = 0;
                        viewContext.HttpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(this), switchWriter, true /* preserveForm */);
                    }
                    finally {
                        // Restore the original _nextId in case this isn't actually the outermost view, since resetting
                        // the _nextId may now cause trace ID conflicts in the outer view.
                        _nextId = originalNextId;
                    }
                }
                else {
                    ProcessRequest(HttpContext.Current);
                }
            }
        }
Exemple #5
0
 public WriterScope(SwitchWriter switchWriter, TextWriter writerToRestore)
 {
     _switchWriter    = switchWriter;
     _writerToRestore = writerToRestore;
 }