Exemplo n.º 1
0
        public static HttpHandlerResult ExternalView(string name, object param)
        {
            if (Path.DirectorySeparatorChar != '\\')
            {
                name = name.Replace("\\", Path.DirectorySeparatorChar.ToString());
            }

            var filename = Path.GetFullPath(name);

            if (!File.Exists(filename))
            {
                throw new ApplicationException($"External View '{filename}' could not be found!");
            }

            string content;

            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var reader = new StreamReader(stream)) {
                    content = reader.ReadToEnd();
                }

            var moustache = new HtmlEngine();

            content = moustache.Process(content, ToDictionary(param));

            return(new HttpHandlerResult {
                StatusCode = (int)HttpStatusCode.OK,
                StatusDescription = "OK.",
                Content = content,
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a named view from the <see cref="ViewCollection"/>.
        /// </summary>
        /// <param name="name">The name of the view.</param>
        /// <param name="param">Optional view-model object.</param>
        public static HttpHandlerResult View(HttpReceiverContext context, string name, object param = null)
        {
            if (!context.Views.TryFind(name, out var content))
            {
                throw new ApplicationException($"View '{name}' was not found!");
            }

            var engine = new HtmlEngine(context.Views)
            {
                UrlRoot = context.ListenerPath,
            };

            content = engine.Process(content, param);

            return(new HttpHandlerResult {
                StatusCode = (int)HttpStatusCode.OK,
                StatusDescription = "OK.",
            }.SetText(content));
        }