Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Rendering view from database (cshtml template) using MVC 6

$
0
0

In mvc 5 it was possible to create views at runtime (see code below).

I used it to store the cshtml in a database -or- just in code. How do I implement this in MVC 6?

Stephan

public static class ViewFactory
    {
        private static bool _registered;
        private static object _lock = new object();
        private static readonly Encoding UTF8NoBOM = new UTF8Encoding(false, true);
        private static Dictionary<string, VirtualFile> _viewDictionary = new Dictionary<string, VirtualFile>();

        public class ViewVirtualFile : VirtualFile
        {
            private string _content;

            public ViewVirtualFile(string path, string content)
                : base(path)
            {
                _content = content;
            }

            public override System.IO.Stream Open()
            {
                var ms = new MemoryStream();
                using (var streamWriter = new StreamWriter(ms, UTF8NoBOM, 4096, true))
                {
                    streamWriter.Write(_content);
                }
                ms.Position = 0;
                return ms;
            }
        }

        public class ViewVirtualPathProvider : VirtualPathProvider
        {
            public ViewVirtualPathProvider()
            {
            }

            public override bool FileExists(string virtualPath)
            {
                if (!virtualPath.StartsWith("/ViewFactory"))
                {
                    return false;
                }
                lock (_viewDictionary)
                {
                    return _viewDictionary.ContainsKey(virtualPath);
                }
            }

            public override VirtualFile GetFile(string virtualPath)
            {
                lock (_viewDictionary)
                {
                    return _viewDictionary[virtualPath];
                }
            }
        }

        private static void EnsureRegistered()
        {
            lock (_lock)
            {
                if (!_registered)
                {
                    HostingEnvironment.RegisterVirtualPathProvider(new ViewVirtualPathProvider());
                    _registered = true;
                }
            }
        }

        public static string GetViewName(string cshtml, Type modelType = null)
        {
            EnsureRegistered();

            var viewName = "/ViewFactory" + cshtml.GetMD5Hash().ToString("N") + ".cshtml";

            lock (_lock)
            {
                if (!_viewDictionary.ContainsKey(viewName))
                {
                    var baseClass = "System.Web.Mvc.WebViewPage" + (modelType == null ? "" : "<" + modelType.FullName + ">");
                    _viewDictionary.Add(viewName, new ViewVirtualFile(viewName, "@inherits " + baseClass + "\n" + cshtml));
                }
            }

            return viewName;
        }
    }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>