Пример #1
0
        protected async Task GetErrorPage(StreamWriter writer, Exception error, SourceLocation location)
        {
            var fileSystem = new EmbeddedResourceFileSystem(typeof(WebInitializer).Assembly);
            string content;
            using (var reader = new StreamReader(fileSystem.OpenRead(PathInfo.Create("Core/error_partial.html"))))
            {
                content = await reader.ReadToEndAsync();
            }

            var templateInfo = new StringTemplateInfo("error", content);

            var view = await ((IViewEngine)Resolver.GetService(typeof(IViewEngine)))
	            .CreateViewAsync(templateInfo, typeof(ErrorViewModel)).ConfigureAwait(false);

            if (location == null)
            {
                var modelWithoutLocation = new ErrorViewModel
                {
                    ErrorMessage = error.Message,
                    Details = error.StackTrace
                };
                view.Render(modelWithoutLocation, new RenderingContext(writer));
                return;
            }

            var templateRepository = (ITemplateRepository)this.Resolver.GetService(typeof(ITemplateRepository));
            var sourceTemplate = await templateRepository.GetTemplateAsync(location.TemplateId).ConfigureAwait(false);
            string sourceTemplateSource;

            using (var reader = new StreamReader(sourceTemplate.Open()))
            {
                sourceTemplateSource = await reader.ReadToEndAsync().ConfigureAwait(false);
            }

            var model = new ErrorViewModel
            {
                TemplateId = location.TemplateId,
                ErrorMessage = error.Message,
                Details = error.StackTrace,
                Before = sourceTemplateSource.Substring(0, location.Index),
                Node = sourceTemplateSource.Substring(location.Index, location.Length),
                After = sourceTemplateSource.Substring(location.Index + location.Length),
                Text = HttpUtility.JavaScriptStringEncode(sourceTemplateSource),
                Range = GetRange(sourceTemplateSource, location)
            };

            view.Render(model, new RenderingContext(writer));
        }
        public static string ExecuteJavascript(string view, object model, string viewName)
        {
            using (var context = new JavascriptContext())
            {
                var fileSystem = new EmbeddedResourceFileSystem(typeof (WebInitializer).Assembly);

                using (var reader = new StreamReader(fileSystem.OpenRead(PathInfo.Create("Core/js/ViewEngine.js"))))
                {
                    context.Run(reader.ReadToEnd());
                }

                context.Run("var repo = { actions: {}, register: function(name, action) { this.actions[name] = action; } };");
                context.Run(view);

                context.SetParameter("m", model);
                context.Run(
                    "var ctx = new Tcn.StringRenderingContext(); repo.actions[\""+ viewName +"\"].render(ctx, m); var result = ctx.out;");

                return (string) context.GetParameter("result");
            }
        }