예제 #1
0
        /// <summary>
        /// Renderiza o conteúdo.
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="writer"></param>
        public void Render(System.Web.Mvc.ViewContext viewContext, System.IO.TextWriter writer)
        {
            if (IsRazorFile)
            {
                try
                {
                    var cache = _virtualFile as ITemplateCacheSupport;
                    RazorEngine.Templating.ITemplateKey templateKey = null;
                    if (cache != null)
                    {
                        if (cache.TemplateKey == null)
                        {
                            templateKey = EngineService.GetKey(_virtualFile.VirtualPath.ToLower(), RazorEngine.Templating.ResolveType.Include);
                            cache.Register(templateKey);
                        }
                        else
                        {
                            templateKey = cache.TemplateKey;
                        }
                    }
                    else
                    {
                        templateKey = EngineService.GetKey(_virtualFile.VirtualPath.ToLower(), RazorEngine.Templating.ResolveType.Include);
                    }
                    RazorEngine.Templating.DynamicViewBag viewBag = new CustomDynamicViewBag(viewContext.ViewData)
                    {
                        ViewContext = viewContext
                    };
                    var modelType = _model != null?_model.GetType() : null;

                    if (!EngineService.IsTemplateCached(templateKey, modelType))
                    {
                        try
                        {
                            EngineService.Compile(templateKey, modelType);
                        }
                        catch (Exception ex)
                        {
                            viewContext.HttpContext.Response.StatusCode = 501;
                            writer.Write(ex.ToString());
                            return;
                        }
                    }
                    EngineService.Run(templateKey, writer, modelType, null, viewBag);
                    return;
                }
                catch (Exception ex)
                {
                    viewContext.HttpContext.Response.StatusCode = 501;
                    writer.Write(ex.ToString());
                    return;
                }
            }
            using (var stream = _virtualFile.Open())
            {
                var reader = new System.IO.StreamReader(stream);
                writer.Write(reader.ReadToEnd());
                writer.Flush();
            }
        }
예제 #2
0
        /// <summary>
        /// Renderiza o conteúdo.
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="writer"></param>
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            if (_virtualFile.Name.EndsWith(".cshtml", StringComparison.InvariantCultureIgnoreCase) || _virtualFile.Name.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    var cache = _virtualFile as ITemplateCacheSupport;
                    RazorEngine.Templating.ITemplateKey templateKey = null;
                    if (cache != null)
                    {
                        if (cache.TemplateKey == null)
                        {
                            templateKey = EngineService.GetKey(_virtualFile.VirtualPath.ToLower(), RazorEngine.Templating.ResolveType.Global);
                            cache.Register(templateKey);
                        }
                        else
                        {
                            templateKey = cache.TemplateKey;
                        }
                    }
                    else
                    {
                        templateKey = EngineService.GetKey(_virtualFile.VirtualPath.ToLower(), RazorEngine.Templating.ResolveType.Global);
                    }
                    var modelType = _model != null?_model.GetType() : null;

                    if (!EngineService.IsTemplateCached(templateKey, modelType))
                    {
                        try
                        {
                            EngineService.Compile(templateKey, modelType);
                        }
                        catch (Exception ex)
                        {
                            viewContext.HttpContext.Response.StatusCode = 501;
                            writer.Write(ex.ToString());
                            return;
                        }
                    }
                    RazorEngine.Templating.DynamicViewBag viewBag = new CustomDynamicViewBag(viewContext.ViewData)
                    {
                        ViewContext = viewContext
                    };
                    if (string.IsNullOrEmpty(viewContext.RequestContext.HttpContext.Response.ContentType))
                    {
                        string contentType = null;
                        if (!string.IsNullOrEmpty(contentType))
                        {
                            var path  = _virtualFile.VirtualPath;
                            var index = path.LastIndexOf('.');
                            if (index >= 0)
                            {
                                var extension = path.Substring(index);
                                contentType = Colosoft.Web.ExtendedHtmlUtility.TranslateContentType(extension);
                            }
                        }
                        viewContext.RequestContext.HttpContext.Response.ContentType = contentType;
                    }
                    EngineService.Run(templateKey, writer, modelType, _model, viewBag);
                    writer.Flush();
                    return;
                }
                catch (Exception ex)
                {
                    viewContext.HttpContext.Response.StatusCode = 501;
                    writer.Write(ex.ToString());
                    return;
                }
            }
            using (var stream = _virtualFile.Open())
            {
                var reader = new System.IO.StreamReader(stream);
                writer.Write(reader.ReadToEnd());
                writer.Flush();
            }
        }