public string RenderViewToString(ActionContext actionContext, string viewPath, object model) { // Find the view var viewEngineResult = ViewEngine.FindView(actionContext, viewPath, false); if (!viewEngineResult.Success) { throw new InvalidOperationException($"Couldn't find view '{viewPath}'"); } // Use a stringwriter to write the output using (var output = new StringWriter()) { // Create the context to render the view with var viewContext = new ViewContext( actionContext, viewEngineResult.View, new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }, new TempDataDictionary(actionContext.HttpContext, TempDataProvider), output, new HtmlHelperOptions()); viewEngineResult.View.RenderAsync(viewContext).GetAwaiter().GetResult(); return(output.ToString()); } }
public async Task <IViewComponentResult> InvokeAsync(string nameOfView = null, IDictionary <string, object> customViewData = null, bool warnOnViewNotFound = true) { if (customViewData != null) { foreach (var vd in customViewData) { ViewData[vd.Key] = vd.Value; } } var pth = $"Components/RequiredSection/{(!string.IsNullOrEmpty(nameOfView)?nameOfView:"Default")}"; if (ViewEngine.FindView(ViewContext, pth, false).Success) { if (string.IsNullOrEmpty(nameOfView)) { return(View()); } else { return(View(viewName: nameOfView)); } } if (warnOnViewNotFound) { logger.Log(LogLevel.Warning, "The Component-View {pth} was not found.", pth); return(Content(string.Empty)); } throw new InvalidOperationException($"The Component-View {pth} was not found."); }
private string GetViewName(DefinitionRegistry registry, IDefinition definition) { var definitionSystemName = definition.SystemName; if (ViewEngine.FindView(ViewContext, definitionSystemName, false).Success) { return(definitionSystemName); } return(GetDefaultViewName(registry, definition)); }
IView CreateView(string viewName, ActionContext actionContext) { var viewResult = ViewEngine.FindView(actionContext, viewName, true); if (!viewResult.Success) { throw new Exception($"Email not found for {viewName}. Locations searched: {Environment.NewLine} {string.Join(Environment.NewLine, viewResult.SearchedLocations)}"); } return(viewResult.View); }
public string Render <TModel>(string name, TModel model) { var httpContext = new DefaultHttpContext { RequestServices = ServiceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewEngineResult = ViewEngine.FindView(actionContext, name, false); if (!viewEngineResult.Success) { throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name)); } var view = viewEngineResult.View; using (var output = new StringWriter()) { var viewContext = new ViewContext( actionContext, view, new ViewDataDictionary <TModel>( metadataProvider: new EmptyModelMetadataProvider(), modelState: new ModelStateDictionary()) { Model = model }, new TempDataDictionary( actionContext.HttpContext, TempDataProvider), output, new HtmlHelperOptions()); view.RenderAsync(viewContext).GetAwaiter().GetResult(); return(output.ToString()); } }
public async Task <string> RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) { viewName = ControllerContext.RouteData.Values["action"] as string; } ViewData.Model = model; using (var sw = new StringWriter()) { var actionContext = new ActionContext(HttpContext, RouteData, new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor()); var viewResult = ViewEngine.FindView(actionContext, viewName, false); var viewContext = new Microsoft.AspNetCore.Mvc.Rendering.ViewContext(actionContext, viewResult.View, ViewData, TempData, sw, new Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelperOptions { }); viewContext.ViewBag.AlternateLayout = "~/Views/PartialLayout.cshtml"; await viewResult.View.RenderAsync(viewContext); //viewResult.View.Render(viewContext, sw); return(sw.GetStringBuilder().ToString()); } }