コード例 #1
0
        /// <summary>Ensures that compiled.</summary>
        ///
        /// <param name="page">The page.</param>
        public virtual void EnsureCompiled(RazorPage page)
        {
            if (page == null)
            {
                return;
            }
            if (page.IsValid)
            {
                return;
            }

            lock (page.SyncRoot)
            {
                if (page.IsValid)
                {
                    return;
                }

                var compileTimer = System.Diagnostics.Stopwatch.StartNew();
                try
                {
                    page.IsCompiling      = true;
                    page.CompileException = null;

                    var type = page.PageHost.Compile();

                    page.PageType = type;

                    page.IsValid = true;

                    compileTimer.Stop();
                    Log.DebugFormat("Compiled Razor page '{0}' in {1}ms.", page.File.Name, compileTimer.ElapsedMilliseconds);
                }
                catch (HttpCompileException ex)
                {
                    page.CompileException = ex;
                }
                finally
                {
                    page.IsCompiling          = false;
                    page.MarkedForCompilation = false;
                }
            }
        }
コード例 #2
0
        /// <summary>Invalidate page.</summary>
        ///
        /// <param name="page">The page.</param>
        public virtual void InvalidatePage(RazorPage page)
        {
            if (page.IsValid || page.IsCompiling)
            {
                lock (page.SyncRoot)
                {
                    page.IsValid = false;
                }
            }

            if (Config.PrecompilePages)
                PrecompilePage(page);
        }
コード例 #3
0
        /// <summary>Ensures that compiled.</summary>
        ///
        /// <param name="page">The page.</param>
        public virtual void EnsureCompiled(RazorPage page)
        {
            if (page == null) return;
            if (page.IsValid) return;

            lock (page.SyncRoot)
            {
                if (page.IsValid) return;

                var compileTimer = System.Diagnostics.Stopwatch.StartNew();
                try
                {
                    page.IsCompiling = true;
                    page.CompileException = null;

                    var type = page.PageHost.Compile();

                    page.PageType = type;

                    page.IsValid = true;

                    compileTimer.Stop();
                    Log.DebugFormat("Compiled Razor page '{0}' in {1}ms.", page.File.Name, compileTimer.ElapsedMilliseconds);
                }
                catch (HttpCompileException ex)
                {
                    page.CompileException = ex;
                }
                finally
                {
                    page.IsCompiling = false;
                    page.MarkedForCompilation = false;
                }
            }
        }
コード例 #4
0
        /// <summary>Precompile page.</summary>
        ///
        /// <param name="page">The page.</param>
        ///
        /// <returns>A Task&lt;RazorPage&gt;</returns>
        protected virtual Task<RazorPage> PrecompilePage(RazorPage page)
        {
            page.MarkedForCompilation = true;

            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    EnsureCompiled(page);

                    if ( page.CompileException != null )
                        Log.ErrorFormat("Precompilation of Razor page '{0}' failed: {1}", page.File.Name, page.CompileException.Message);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Precompilation of Razor page '{0}' failed: {1}", page.File.Name, ex.Message);
                }
                return page;
            });

            if (startupPrecompilationTasks != null )
                startupPrecompilationTasks.Add(task);

            return task;
        }
コード例 #5
0
        /// <summary>Adds a page.</summary>
        ///
        /// <param name="page">The page.</param>
        ///
        /// <returns>A RazorPage.</returns>
        protected virtual RazorPage AddPage(RazorPage page)
        {
            var pagePath = GetDictionaryPagePath(page.PageHost.File);

            this.Pages[pagePath] = page;

            //Views should be uniquely named and stored in any deep folder structure
            if (pagePath.StartsWithIgnoreCase("/views/"))
            {
                var viewName = pagePath.SplitOnLast('.').First().SplitOnLast('/').Last();
                ViewNamesMap[viewName] = pagePath;
            }

            return page;
        }
コード例 #6
0
        /// <summary>Track page.</summary>
        ///
        /// <param name="file">The file.</param>
        ///
        /// <returns>A RazorPage.</returns>
        public virtual RazorPage TrackPage(IVirtualFile file)
        {
            //get the base type.
            var pageBaseType = this.Config.PageBaseType;

            var transformer = new RazorViewPageTransformer(pageBaseType);

            //create a RazorPage
            var page = new RazorPage
            {
                PageHost = new RazorPageHost(PathProvider, file, transformer, new CSharpCodeProvider(), new Dictionary<string, string>()),
                IsValid = false,
                File = file
            };

            //add it to our pages dictionary.
            AddPage(page);

            if (Config.PrecompilePages)
                PrecompilePage(page);
            
            return page;
        }
コード例 #7
0
        private IRazorView CreateRazorPageInstance(IHttpRequest httpReq, IHttpResponse httpRes, object dto, RazorPage razorPage)
        {
            viewManager.EnsureCompiled(razorPage);

            //don't proceed any further, the background compiler found there was a problem compiling the page, so throw instead
            if (razorPage.CompileException != null)
            {
                throw razorPage.CompileException;
            }

            //else, EnsureCompiled() ensures we have a page type to work with so, create an instance of the page
            var page = (IRazorView) razorPage.ActivateInstance();

            page.Init(viewEngine: this, httpReq: httpReq, httpRes: httpRes);

            //deserialize the model.
            PrepareAndSetModel(page, httpReq, dto);
        
            return page;
        }
コード例 #8
0
        /// <summary>Resolve and execute razor page.</summary>
        ///
        /// <param name="httpReq">  The HTTP request.</param>
        /// <param name="httpRes">  The HTTP resource.</param>
        /// <param name="model">    The model.</param>
        /// <param name="razorPage">The razor page.</param>
        ///
        /// <returns>An IRazorView.</returns>
        public IRazorView ResolveAndExecuteRazorPage(IHttpRequest httpReq, IHttpResponse httpRes, object model, RazorPage razorPage=null)
        {
            razorPage = razorPage ?? FindRazorPage(httpReq, model);

            if (razorPage == null)
            {
                httpRes.StatusCode = (int)HttpStatusCode.NotFound;
                return null;
            }

            var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage);

            var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue);
            if (includeLayout)
            {
                var result = ExecuteRazorPageWithLayout(httpReq, httpRes, model, page, () => 
                    httpReq.GetItem(LayoutKey) as string
                    ?? page.Layout
                    ?? DefaultLayoutName);

                using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
                {
                    writer.Write(result.Item2);
                }
                return result.Item1;
            }

            using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
            {
                page.WriteTo(writer);
            }
            return page;
        }
コード例 #9
0
        private IRazorView CreateRazorPageInstance(IHttpRequest httpReq, IHttpResponse httpRes, object dto, RazorPage razorPage)
        {
            viewManager.EnsureCompiled(razorPage);

            //don't proceed any further, the background compiler found there was a problem compiling the page, so throw instead
            if (razorPage.CompileException != null)
            {
                throw razorPage.CompileException;
            }

            //else, EnsureCompiled() ensures we have a page type to work with so, create an instance of the page
            var page = (IRazorView)razorPage.ActivateInstance();

            page.Init(viewEngine: this, httpReq: httpReq, httpRes: httpRes);

            //deserialize the model.
            PrepareAndSetModel(page, httpReq, dto);

            return(page);
        }
コード例 #10
0
        /// <summary>Resolve and execute razor page.</summary>
        ///
        /// <param name="httpReq">  The HTTP request.</param>
        /// <param name="httpRes">  The HTTP resource.</param>
        /// <param name="model">    The model.</param>
        /// <param name="razorPage">The razor page.</param>
        ///
        /// <returns>An IRazorView.</returns>
        public IRazorView ResolveAndExecuteRazorPage(IHttpRequest httpReq, IHttpResponse httpRes, object model, RazorPage razorPage = null)
        {
            razorPage = razorPage ?? FindRazorPage(httpReq, model);

            if (razorPage == null)
            {
                httpRes.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage);

            var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue);

            if (includeLayout)
            {
                var result = ExecuteRazorPageWithLayout(httpReq, httpRes, model, page, () =>
                                                        httpReq.GetItem(LayoutKey) as string
                                                        ?? page.Layout
                                                        ?? DefaultLayoutName);

                using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
                {
                    writer.Write(result.Item2);
                }
                return(result.Item1);
            }

            using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
            {
                page.WriteTo(writer);
            }
            return(page);
        }
コード例 #11
0
ファイル: RazorFormat.cs プロジェクト: Qasemt/NServiceKit
        /// <summary>Renders to HTML.</summary>
        ///
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        ///
        /// <param name="razorPage">The razor page.</param>
        /// <param name="razorView">The razor view.</param>
        /// <param name="model">    The model.</param>
        /// <param name="layout">   The layout.</param>
        ///
        /// <returns>A string.</returns>
        public string RenderToHtml(RazorPage razorPage, out IRazorView razorView, object model = null, string layout = null)
        {
            if (razorPage == null)
                throw new ArgumentNullException("razorPage");

            var mqContext = new MqRequestContext();

            var httpReq = new MqRequest(mqContext);
            if (layout != null)
            {
                httpReq.Items[RazorPageResolver.LayoutKey] = layout;
            }

            var httpRes = new MqResponse(mqContext);

            razorView = PageResolver.ResolveAndExecuteRazorPage(
                httpReq: httpReq,
                httpRes: httpRes,
                model: model,
                razorPage: razorPage);

            var ms = (MemoryStream)httpRes.OutputStream;
            return ms.ToArray().FromUtf8Bytes();
        }
コード例 #12
0
ファイル: RazorFormat.cs プロジェクト: Qasemt/NServiceKit
 /// <summary>Renders to HTML.</summary>
 ///
 /// <param name="razorPage">The razor page.</param>
 /// <param name="model">    The model.</param>
 /// <param name="layout">   The layout.</param>
 ///
 /// <returns>A string.</returns>
 public string RenderToHtml(RazorPage razorPage, object model = null, string layout = null)
 {
     IRazorView razorView;
     return RenderToHtml(razorPage, out razorView, model: model, layout: layout);
 }
コード例 #13
0
ファイル: RazorFormat.cs プロジェクト: Qasemt/NServiceKit
 /// <summary>Process the razor page.</summary>
 ///
 /// <param name="httpReq">    The HTTP request.</param>
 /// <param name="contentPage">The content page.</param>
 /// <param name="model">      The model.</param>
 /// <param name="httpRes">    The HTTP resource.</param>
 public void ProcessRazorPage(IHttpRequest httpReq, RazorPage contentPage, object model, IHttpResponse httpRes)
 {
     PageResolver.ResolveAndExecuteRazorPage(httpReq, httpRes, model, contentPage);
 }