Beispiel #1
0
        private string ExecuteView(ViewLocationResult viewLocationResult, dynamic model)
        {
            //Preprocess the location result and inject the global helper
            TextReader processedContentStream = InjectHelpersToView(viewLocationResult.Contents);

            var      cache = System.Runtime.Caching.MemoryCache.Default;
            ViewBase view  = (ViewBase)cache.Get(viewLocationResult.Location);

            if (view == null)
            {
                view = GetCompiledView <dynamic>(processedContentStream);
                var cachePolicy = new CacheItemPolicy();
                cachePolicy.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                    viewLocationResult.Location
                }));
                cache.Add(new System.Runtime.Caching.CacheItem(viewLocationResult.Location, view), cachePolicy);
            }

            view.Global = _modelProvider.GetGlobalModel();
            view.Model  = model ?? _modelProvider.GetModel(Path.GetFileNameWithoutExtension(viewLocationResult.Location));

            view.RenderPartialImpl = (partialViewName, partialModel)
                                     => new HtmlStringLiteral(ExecuteView(Conventions.PartialsPrefix + partialViewName, partialModel));
            view.Execute();

            if (string.IsNullOrEmpty(view.Layout))
            {
                return(view.GetContents());
            }
            else
            {
                string layout = ExecuteView(String.Format("{0}{1}", Conventions.LayoutsPrefix, view.Layout), model);

                return(layout.Replace("{{content}}", view.GetContents()));
            }
        }