public virtual void InvalidatePage(RazorPage page) { if (page.IsValid || page.IsCompiling) { lock (page.SyncRoot) { page.IsValid = false; } } if (Config.PrecompilePages) PrecompilePage(page); }
public virtual void InvalidatePage(RazorPage page, bool compile = true) { if (page.IsValid || page.IsCompiling) { lock (page.SyncRoot) { page.IsValid = false; } } if (compile) PrecompilePage(page); }
protected virtual RazorPage AddPage(RazorPage page, string pagePath = null) { pagePath = pagePath != null ? GetDictionaryPagePath(pagePath) : GetDictionaryPagePath(page.PageHost.File); Pages[pagePath] = page; //Views should be uniquely named and stored in any deep folder structure if (pagePath.StartsWithIgnoreCase("views/") && !pagePath.EndsWithIgnoreCase(DefaultLayoutFile)) { var viewName = pagePath.LastLeftPart('.').LastRightPart('/'); ViewNamesMap[viewName] = pagePath; } return(page); }
public void EnsureCompiled(RazorPage page, IHttpResponse response) { if (page == null) { return; } if (page.IsValid) { return; } var type = page.PageHost.Compile(); page.PageType = type; page.IsValid = true; }
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; } } }
private Tuple <IRazorView, string> ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, object model, IRazorView pageInstance, Func <string> layout) { using (var ms = MemoryStreamFactory.GetStream()) { var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom); //ms disposed in using //child page needs to execute before master template to populate ViewBags, sections, etc try { pageInstance.WriteTo(childWriter); } catch (StopExecutionException) { } catch (Exception ex) { ex = ex.UnwrapIfSingleException(); if (!(ex is StopExecutionException)) { throw; } } if (httpRes.IsClosed) { return(null); } var childBody = ms.ReadToEnd(); var layoutName = layout(); if (!string.IsNullOrEmpty(layoutName)) { var layoutPage = viewManager.GetLayoutPage(layoutName, razorPage, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(pageInstance, childBody); return(ExecuteRazorPageWithLayout(layoutPage, httpReq, httpRes, model, layoutView, () => layoutView.Layout)); } } return(Tuple.Create(pageInstance, childBody)); } }
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); return(page); }
public virtual RazorPage TrackPage(Type pageType) { var pageBaseType = this.Config.PageBaseType; var transformer = new RazorViewPageTransformer(pageBaseType); var pagePath = pageType.FirstAttribute <VirtualPathAttribute>().VirtualPath.TrimStart('~'); var file = GetVirtualFile(pagePath); var page = new RazorPage { PageHost = file != null?CreatePageHost(file, transformer) : null, PageType = pageType, IsValid = true, File = file, VirtualPath = pagePath, }; AddPage(page, pagePath); return(page); }
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); return page; }
public virtual RazorPage TrackPage(Type pageType) { var pageBaseType = this.Config.PageBaseType; var transformer = new RazorViewPageTransformer(pageBaseType); var pagePath = pageType.FirstAttribute <VirtualPathAttribute>().VirtualPath.TrimStart('~'); var file = GetVirtualFile(pagePath); var page = new RazorPage { PageHost = file != null ? new RazorPageHost(PathProvider, file, transformer, new CSharpCodeProvider(), new Dictionary <string, string>()) : null, PageType = pageType, IsValid = true, File = file, VirtualPath = pagePath, }; AddPage(page, pagePath); return(page); }
protected virtual void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { try { var file = views.GetVirtualFile(e.FullPath); if (file == null || !views.IsWatchedFile(file)) { return; } RazorPage page = views.GetPage(file); if (page != null) { views.InvalidatePage(page); } } catch (Exception ex) { Log.Warn("FileSystemWatcher_Changed error: ", ex); } }
private Tuple <IRazorView, string> ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, object model, IRazorView pageInstance, Func <string> layout) { using (var ms = new MemoryStream()) { using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc try { pageInstance.WriteTo(childWriter); } catch (StopExecutionException ignore) {} if (httpRes.IsClosed) { return(null); } var childBody = ms.ToArray().FromUtf8Bytes(); var layoutName = layout(); if (!string.IsNullOrEmpty(layoutName)) { var layoutPage = viewManager.GetLayoutPage(layoutName, razorPage, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(pageInstance, childBody); return(ExecuteRazorPageWithLayout(layoutPage, httpReq, httpRes, model, layoutView, () => layoutView.Layout)); } } return(Tuple.Create(pageInstance, childBody)); } } }
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; } } }
private IRazorView CreateRazorPageInstance(IHttpRequest httpReq, IHttpResponse httpRes, object dto, RazorPage razorPage) { EnsureCompiled(razorPage, httpRes); //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; }
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; }
public string RenderToHtml(RazorPage razorPage, object model = null, string layout = null) { IRazorView razorView; return RenderToHtml(razorPage, out razorView, model: model, layout: layout); }
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); } using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom)) { var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage); var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue); if (includeLayout) { using (var ms = new MemoryStream()) using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc page.WriteTo(childWriter); var layout = httpReq.GetItem(LayoutKey) as string ?? page.Layout ?? DefaultLayoutName; var childBody = ms.ToArray().FromUtf8Bytes(); var layoutPage = this.viewManager.GetPageByName(layout, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(page, childBody); layoutView.WriteTo(writer); return(layoutView); } writer.Write(childBody); return(page); } } page.WriteTo(writer); return(page); } }
public IRazorView ExecuteRazorPage(IRequest httpReq, IResponse httpRes, object model, RazorPage razorPage) { 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(razorPage, httpReq, httpRes, model, page, () => httpReq.GetItem(LayoutKey) as string ?? page.Layout ?? DefaultLayoutName); if (httpRes.IsClosed) { return(result != null ? result.Item1 : null); } var layoutWriter = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom); var html = HtmlFilter(result.Item2); layoutWriter.Write(html); layoutWriter.Flush(); return(result.Item1); } var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom); page.WriteTo(writer); writer.Flush(); return(page); }
private IRazorView CreateRazorPageInstance(IHttpRequest httpReq, IHttpResponse httpRes, object dto, RazorPage razorPage) { EnsureCompiled(razorPage, httpRes); //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); }
public virtual RazorPage TrackPage(Type pageType) { var pageBaseType = this.Config.PageBaseType; var transformer = new RazorViewPageTransformer(pageBaseType); var pagePath = pageType.FirstAttribute<VirtualPathAttribute>().VirtualPath.TrimStart('~'); var file = GetVirtualFile(pagePath); var page = new RazorPage { PageHost = file != null ? new RazorPageHost(PathProvider, file, transformer, new CSharpCodeProvider(), new Dictionary<string, string>()) : null, PageType = pageType, IsValid = true, File = file, VirtualPath = pagePath, }; AddPage(page, pagePath); return page; }
public IRazorView ExecuteRazorPage(IRequest httpReq, IResponse httpRes, object model, RazorPage razorPage) { if (razorPage == null) { httpRes.StatusCode = (int)HttpStatusCode.NotFound; return null; } try { var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage); var includeLayout = !(httpReq.GetParam(Keywords.Format) ?? "").Contains(Keywords.Bare); if (includeLayout) { var result = ExecuteRazorPageWithLayout(razorPage, httpReq, httpRes, model, page, () => httpReq.GetItem(LayoutKey) as string ?? page.Layout ?? DefaultLayoutName); if (httpRes.IsClosed) return result != null ? result.Item1 : null; var layoutWriter = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom); var html = HtmlFilter(result.Item2); layoutWriter.Write(html); layoutWriter.Flush(); return result.Item1; } var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom); page.WriteTo(writer); writer.Flush(); return page; } finally { RequestContext.Instance.ReleaseDisposables(); } }
public IRazorView ResolveAndExecuteRazorPage(IHttpRequest httpReq, IHttpResponse httpRes, object model, RazorPage razorPage=null) { var viewName = httpReq.GetItem(ViewKey) as string; if (razorPage == null && viewName != null) { razorPage = this.viewManager.GetPageByName(viewName); } else { razorPage = razorPage ?? this.viewManager.GetPageByName(httpReq.OperationName) //Request DTO ?? this.viewManager.GetPage(httpReq, model); // Response DTO } if (razorPage == null) { httpRes.StatusCode = (int)HttpStatusCode.NotFound; return null; } using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom)) { var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage); var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue); if (includeLayout) { using (var ms = new MemoryStream()) using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc page.WriteTo(childWriter); var layout = httpReq.GetItem(LayoutKey) as string ?? page.Layout ?? DefaultLayoutName; var childBody = ms.ToArray().FromUtf8Bytes(); var layoutPage = this.viewManager.GetPageByName(layout, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(page, childBody); layoutView.WriteTo(writer); return layoutView; } writer.Write(childBody); return page; } } page.WriteTo(writer); return page; } }
private IRazorView CreateRazorPageInstance(IRequest httpReq, IResponse 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) { if (Text.Env.IsMono) { //Additional debug info Working around not displaying default exception in IHttpAsyncHandler var errors = razorPage.CompileException.Results.Errors; for (var i = 0; i < errors.Count; i++) { var error = errors[i]; Log.Debug("{0} Line: {1}:{2}:".Fmt(error.FileName, error.Line, error.Column)); Log.Debug("{0}: {1}".Fmt(error.ErrorNumber, error.ErrorText)); } } 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; }
private Tuple<IRazorView, string> ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, object model, IRazorView pageInstance, Func<string> layout) { using (var ms = new MemoryStream()) { using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc try { pageInstance.WriteTo(childWriter); } catch (StopExecutionException ignore) {} if (httpRes.IsClosed) return null; var childBody = ms.ToArray().FromUtf8Bytes(); var layoutName = layout(); if (!string.IsNullOrEmpty(layoutName)) { var layoutPage = viewManager.GetLayoutPage(layoutName, razorPage, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(pageInstance, childBody); return ExecuteRazorPageWithLayout(layoutPage, httpReq, httpRes, model, layoutView, () => layoutView.Layout); } } return Tuple.Create(pageInstance, childBody); } } }
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 = CreatePageHost(file, transformer), IsValid = false, File = file, VirtualPath = file.VirtualPath, }; //add it to our pages dictionary. AddPage(page); if (Config.PrecompilePages.GetValueOrDefault()) PrecompilePage(page); return page; }
protected virtual RazorPage AddPage(RazorPage page, string pagePath = null) { pagePath = pagePath != null ? GetDictionaryPagePath(pagePath) : GetDictionaryPagePath(page.PageHost.File); Pages[pagePath] = page; //Views should be uniquely named and stored in any deep folder structure if (pagePath.StartsWithIgnoreCase("views/") && !pagePath.EndsWithIgnoreCase(DefaultLayoutFile)) { var viewName = pagePath.SplitOnLast('.').First().SplitOnLast('/').Last(); ViewNamesMap[viewName] = pagePath; } return page; }
public virtual void EnsureCompiled(RazorPage page) { if (page == null) { return; } if (CheckLastModifiedForChanges && page.IsValid) { lock (page.SyncRoot) { var prevLastModified = page.File.LastModified; page.File.Refresh(); page.IsValid = prevLastModified == page.File.LastModified; } } if (page.IsValid) { return; } if (page.PageHost == null) { Logger.WarnFormat("Could not find virtualPath for compiled Razor page '{0}'.", page.VirtualPath); 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(); if (Logger.IsDebugEnabled) { Logger.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; } } }
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; }
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); }
public IRazorView ExecuteRazorPage(IRequest httpReq, Stream outputStream, object model, RazorPage razorPage) { var httpRes = httpReq.Response; if (razorPage == null) { httpRes.StatusCode = (int)HttpStatusCode.NotFound; return(null); } try { var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage); var includeLayout = !(httpReq.GetParam(Keywords.Format) ?? "").Contains(Keywords.Bare); if (includeLayout) { var result = ExecuteRazorPageWithLayout(razorPage, httpReq, httpRes, model, page, () => httpReq.GetItem(Keywords.Template) as string ?? page.Layout ?? DefaultLayoutName); if (httpRes.IsClosed) { return(result?.Item1); } var layoutWriter = new StreamWriter(outputStream, UTF8EncodingWithoutBom); var html = HtmlFilter(result.Item2); layoutWriter.Write(html); layoutWriter.Flush(); return(result.Item1); } var writer = new StreamWriter(outputStream, UTF8EncodingWithoutBom); page.WriteTo(writer); writer.Flush(); return(page); } finally { RequestContext.Instance.ReleaseDisposables(); } }
public void ProcessRazorPage(IRequest httpReq, RazorPage contentPage, object model, IResponse httpRes) { PageResolver.ExecuteRazorPage(httpReq, httpRes, model, contentPage); }
public virtual RazorPage TrackPage(Type pageType) { var pagePath = pageType.FirstAttribute<VirtualPathAttribute>().VirtualPath.TrimStart('~'); var page = new RazorPage { PageType = pageType, IsValid = true }; AddPage(page, pagePath); return page; }
public virtual RazorPage TrackPage(Type pageType) { var pageBaseType = this.Config.PageBaseType; var transformer = new RazorViewPageTransformer(pageBaseType); var pagePath = pageType.FirstAttribute<VirtualPathAttribute>().VirtualPath.TrimStart('~'); var file = GetVirtualFile(pagePath); var page = new RazorPage { PageHost = file != null ? CreatePageHost(file, transformer) : null, PageType = pageType, IsValid = true, File = file, VirtualPath = pagePath, }; AddPage(page, pagePath); return page; }
public string RenderToHtml(RazorPage razorPage, object model = null, string layout = null) { IRazorView razorView; var result = RenderToHtml(razorPage, out razorView, model: model, layout: layout); using (razorView) { return result; } }
public virtual RazorPage GetLayoutPage(string layoutName, RazorPage page, IRequest request, object dto) { var layoutFile = Path.ChangeExtension(layoutName, Config.RazorFileExtension); // layoutName may or may not contain the .cshtml extension, the below forces it not to. layoutName = Path.GetFileNameWithoutExtension(layoutFile); var contextRelativePath = page.VirtualPath; string contextParentDir = contextRelativePath; do { contextParentDir = (contextParentDir ?? "").SplitOnLast('/').First(); var path = CombinePaths(contextParentDir, layoutFile); var layoutPage = GetPage(path); if (layoutPage != null) return layoutPage; } while (!string.IsNullOrEmpty(contextParentDir) && contextParentDir.Contains('/')); if (layoutName != RazorPageResolver.DefaultLayoutName) return GetViewPage(layoutName); return GetPage(CombinePaths("/views/shared/", layoutFile)) ?? GetPage(CombinePaths("/views/", layoutFile)); //backwards compatibility fallback }
public string RenderToHtml(RazorPage razorPage, out IRazorView razorView, object model = null, string layout = null) { if (razorPage == null) throw new ArgumentNullException("razorPage"); var httpReq = new BasicRequest(); if (layout != null) { httpReq.Items[RazorPageResolver.LayoutKey] = layout; } razorView = PageResolver.ExecuteRazorPage(httpReq, httpReq.Response, model, razorPage); var ms = (MemoryStream)httpReq.Response.OutputStream; return ms.ToArray().FromUtf8Bytes(); }
public virtual void EnsureCompiled(RazorPage page) { if (page == null) return; if (CheckLastModifiedForChanges && page.IsValid) { lock (page.SyncRoot) { var prevLastModified = page.File.LastModified; page.File.Refresh(); page.IsValid = prevLastModified == page.File.LastModified; } } if (page.IsValid) return; if (page.PageHost == null) { Log.WarnFormat("Could not find virtualPath for compiled Razor page '{0}'.", page.VirtualPath); 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(); if (Log.IsDebugEnabled) 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; } } }
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; } using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom)) { var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage); var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue); if (includeLayout) { var result = ExecuteRazorPageWithLayout(httpReq, httpRes, model, page, () => { return httpReq.GetItem(LayoutKey) as string ?? page.Layout ?? DefaultLayoutName; }); writer.Write(result.Item2); return result.Item1; } page.WriteTo(writer); return page; } }
private IRazorView CreateRazorPageInstance(IRequest httpReq, IResponse 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 var pageCompileEx = razorPage.CompileException; if (pageCompileEx != null) { if (Text.Env.IsMono) { //Additional debug info Working around not displaying default exception in IHttpAsyncHandler var errors = pageCompileEx.Results.Errors; for (var i = 0; i < errors.Count; i++) { var error = errors[i]; Log.Debug("{0} Line: {1}:{2}:".Fmt(error.FileName, error.Line, error.Column)); Log.Debug("{0}: {1}".Fmt(error.ErrorNumber, error.ErrorText)); } } throw pageCompileEx; } //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); }
public void EnsureCompiled(RazorPage page, IHttpResponse response) { if (page == null) return; if (page.IsValid) return; var type = page.PageHost.Compile(); page.PageType = type; page.IsValid = true; }