/// <summary> /// Includes the template with the specified key /// </summary> /// <param name="key">Key used to resolve a template</param> /// <param name="model">Template model</param> public async Task IncludeAsync(string key, object model = null) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } if (this.PageLookup == null) { throw new RazorLightException("Can't locate a page as PageLookup is not set"); } PageLookupResult pageResult = PageLookup.GetPage(key); if (pageResult.Success) { TemplatePage page = pageResult.ViewEntry.PageFactory(); page.PageContext = new PageContext(this.PageContext.ViewBag) { Writer = this.PageContext.Writer }; if (model != null) { var modelTypeInfo = new ModelTypeInfo(model.GetType()); page.PageContext.ModelTypeInfo = modelTypeInfo; object pageModel = modelTypeInfo.CreateTemplateModel(model); page.SetModel(pageModel); } await page.ExecuteAsync(); } }
/// <summary> /// Parses a template with a given <paramref name="key" /> /// </summary> /// <param name="key">Key used to resolve a template</param> /// <param name="model">Template model</param> /// <param name="modelType">Type of the model</param> /// <param name="viewBag">Dynamic ViewBag (can be null)</param> /// <returns>Returns parsed string</returns> /// <remarks>Result is stored in cache</remarks> public string Parse(string key, object model, Type modelType, ExpandoObject viewBag) { PageLookupResult result = pageLookup.GetPage(key); if (!result.Success) { throw new RazorLightException($"Can't find a view with a specified key ({key})"); } var pageContext = new PageContext(viewBag) { ModelTypeInfo = new ModelTypeInfo(modelType) }; foreach (var viewStartPage in result.ViewStartEntries) { pageContext.ViewStartPages.Add(viewStartPage.PageFactory()); } TemplatePage page = result.ViewEntry.PageFactory(); page.PageContext = pageContext; return(RunTemplate(page, model)); }
private TemplatePage GetLayoutPage(string layoutKey) { PageLookupResult layoutPageResult = pageLookup.GetPage(layoutKey); if (!layoutPageResult.Success) { throw new RazorLightException($"Layout cannot be located ({layoutKey})"); } TemplatePage layoutPage = layoutPageResult.ViewEntry.PageFactory(); return(layoutPage); }