public static string RenderViewAreaToString(string controllerName, string viewName, string areaName, object viewModel, RequestContext rctx) { try { using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); routeData.Values.Add("Area", areaName); routeData.DataTokens["area"] = areaName; var fakeControllerContext = new ControllerContext(rctx, new BaseController()); //new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController()); fakeControllerContext.RouteData = routeData; var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewModel), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return writer.GetStringBuilder().ToString(); //use example //String renderedHTML = RenderViewToString("Email", "MyHTMLView", myModel ); //where file MyHTMLView.cstml is stored in Views/Email/MyHTMLView.cshtml. Email is a fake controller name. } } catch (Exception ex) { //do your exception handling here throw ex; } }
public async Task <string> RenderViewToStringAsync(string viewPath, object model, ITempDataDictionary tempData) { var httpContext = new DefaultHttpContext { RequestServices = ServiceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); using (var sw = new StringWriter()) { var viewResult = RazorViewEngine.FindView(actionContext, viewPath, false); if (viewResult.View == null) { throw new ArgumentNullException($"{viewPath} does not match any available view"); } var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, tempData, sw, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return(sw.ToString()); } }
/// <summary> /// Renders the view to string. /// </summary> /// <param name="viewName">Name of the view.</param> /// <param name="viewData">The view data.</param> /// <param name="controller">The controller.</param> /// <param name="additionalData">The additional data.</param> /// <returns></returns> public static string RenderViewToString(string viewName, object viewData, ControllerBase controller, IDictionary <string, object> additionalData) { try { HttpContextBase contextBase = new HttpContextWrapper(HttpContext.Current); TempDataDictionary tempData = new TempDataDictionary(); foreach (var item in additionalData) { tempData[item.Key] = item.Value; } var routeData = new RouteData(); routeData.Values.Add("controller", controller.GetType().Name.Replace("Controller", "")); var controllerContext = new ControllerContext(contextBase, routeData, controller); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false); var writer = new StringWriter(); var viewContext = new ViewContext(controllerContext, razorViewResult.View, new ViewDataDictionary(viewData), tempData, writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } catch (Exception ex) { ex.ExceptionValueTracker(viewName, viewData, controller, additionalData); } return(string.Empty); }
protected string ViewToString <T>(string controllerName, string viewPath, T model, bool useCache = false) { try { using (var writer = new StringWriter()) { // Create a new route data var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); // Create fake controller context var fakeControllerContext = new ControllerContext( new HttpContextWrapper( new HttpContext( new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController()); // Create the razor engine var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewPath, "", false); // Create view context var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), writer); // Render view razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } } catch (Exception ex) { throw; } }
internal static string RenderViewToString(string controllerName, string viewName, ViewDataDictionary viewData, bool partial = false) { var context = HttpContext.Current; var contextBase = new HttpContextWrapper(context); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var controllerContext = new ControllerContext(contextBase, routeData, new EmptyController()); var razorViewEngine = new RazorViewEngine(); ViewEngineResult razorViewResult = null; if (partial) { razorViewResult = razorViewEngine.FindPartialView(controllerContext, viewName, false); } else { razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false); } var writer = new StringWriter(); var viewContext = new ViewContext(controllerContext, razorViewResult.View, viewData, new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); }
public static string RenderViewToString(string controllerName, string viewName, object viewData) { var context = System.Web.HttpContext.Current; var contextBase = new HttpContextWrapper(context); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var controllerContext = new ControllerContext(contextBase, routeData, new EmptyController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false); var writer = new StringWriter(); var viewContext = new ViewContext(controllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); }
/// <inheritdoc /> public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage) { var viewEngineResult = _engine.FindView(context, viewName, isMainPage); context.HttpContext.SetView(viewEngineResult.View.Path); if (isMainPage) { context.HttpContext.SetMainPageName(viewEngineResult.View.Path); } return(viewEngineResult); }
public static string RenderViewToString(string controllerName, string viewName, object viewData, ControllerContext controllerContext) { using (var writer = new System.IO.StringWriter()) { var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false); var viewContext = new ViewContext(controllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } }
public static string RenderViewToString(string controllerName, string viewName, object viewModel) { using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new BaseController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewModel), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return writer.ToString(); } }
/// <summary> /// Gets markup of a view based on its full path name and model /// </summary> /// <param name="viewName">Name of view to get markup for</param> /// <param name="model">Model to pass to view</param> /// <returns>HTML un-encoded markup of view</returns> public virtual string GetEmailMarkup(string viewName, object model) { ControllerContext controller = GetFakeContext(); using (StringWriter sw = new StringWriter()) { var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controller, viewName, "", false); var viewContext = new ViewContext(controller, razorViewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), sw); razorViewResult.View.Render(viewContext, sw); return(sw.ToString()); } }
public string RenderViewToString(object viewData) { using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", "Inscripcion"); var inscripcionController = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new InscripcionController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(inscripcionController, "SuccessEmail", "", false); var viewContext = new ViewContext(inscripcionController, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } }
private static string RenderViewToString(string controllerName, string viewName, object model) { using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var emailControllerContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), routeData, new MailController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(emailControllerContext, viewName, "", false); var viewContext = new ViewContext(emailControllerContext, razorViewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } }
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { var viewModel = controllerContext.Controller.ViewData.Model; if (controllerContext is ViewContext) { viewModel = ((ViewContext)controllerContext).ViewData.Model; } viewName = TryFindViewFromViewModel(controllerContext.HttpContext.Cache, viewModel) ?? viewName; var result = base.FindView(controllerContext, viewName, masterName, useCache); if (result.View == null && !PreemptPhysicalFiles) { result = razorViewEngine.FindView(controllerContext, viewName, masterName, useCache); } return(result); }
public IHttpActionResult RestorePassword([FromBody] RestorePasswordModel model) { var member = Services.MemberService.GetByUsername(model.Username); if (member == null) { return(NotValid()); } var password = Membership.GeneratePassword(8, 2); Services.MemberService.SavePassword(member, password); string body; using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", "Fake"); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "https://iibb.by", null), new HttpResponse(null))), routeData, new FakeController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, "EmailForgotPassword", "", false); dynamic data = new ExpandoObject(); data.Name = member.Name; data.Password = password; var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(data), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); body = writer.ToString(); } var message = new MailMessage(); message.To.Add(new MailAddress(model.Username)); message.From = new MailAddress("*****@*****.**"); message.Subject = "Восстановление пароля Irene Italiano BOUTIQUE BIJOUTERIE"; message.Body = body; message.IsBodyHtml = true; using (var smtp = new SmtpClient()) { smtp.Send(message); } return(Ok()); }
public void FindView_ReturnsRazorView_IfLookupWasSuccessful() { // Arrange var pageFactory = new Mock <IRazorPageFactory>(); var page = Mock.Of <IRazorPage>(); pageFactory.Setup(p => p.CreateInstance(It.IsAny <string>())) .Returns(Mock.Of <IRazorPage>()); var viewEngine = new RazorViewEngine(pageFactory.Object); var context = GetActionContext(_controllerTestContext); // Act var result = viewEngine.FindView(context, "test-view"); // Assert Assert.True(result.Success); Assert.IsAssignableFrom <IRazorView>(result.View); Assert.Equal("/Views/bar/test-view.cshtml", result.ViewName); }
public static string RenderView(string controllerName, string viewName, object viewData) { //Create memory writer var writer = new StringWriter(); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); //Create fake http context to render the view var fakeRequest = new HttpRequest(null, "http://tempuri.org", null); var fakeResponse = new HttpResponse(null); var fakeContext = new HttpContext(fakeRequest, fakeResponse); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), routeData, new FakeController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); }
public async Task <string> RenderViewToStringAsync(string viewPath, ViewDataDictionary viewData, ITempDataDictionary tempData) { var httpContext = new DefaultHttpContext { RequestServices = ServiceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); using (var sw = new StringWriter()) { ViewEngineResult viewResult = RazorViewEngine.GetView(executingFilePath: viewPath, viewPath: viewPath, isMainPage: false); if (!viewResult.Success) { viewResult = RazorViewEngine.FindView(actionContext, viewPath, false); } if (viewResult.View == null) { Log.Logger.Warning($"{viewPath} does not match any available view"); Log.Logger.Warning("Search location {@SearchedLocations}", viewResult.SearchedLocations); throw new ArgumentNullException($"{viewPath} does not match any available view. View log for detail"); } if (viewResult.View == null) { throw new ArgumentNullException($"{viewPath} does not match any available view"); } var viewContext = new ViewContext( actionContext, viewResult.View, viewData, tempData, sw, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return(sw.ToString()); } }
/// <summary> /// RenderViewToString. /// </summary> public static string RenderViewToString(string controllerName, string viewName, object viewData, bool isOrderSummary = true) { try { using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new VirtualController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString().Replace("/r/n", "").Replace("//\"", "")); } } catch (Exception ex) { // EmailUtil.SendEmail($"[Exception-RenderViewToString]-{viewName}-{ex.Message}", $"{ex.StackTrace}", EmailAddress.ProductOrderSummaryPersonInCharge); throw ex; } }
public static string RenderRazorViewToString(string viewName, object model, ViewDataDictionary viewData = null) { if (viewData == null) { viewData = new ViewDataDictionary(); } viewData.Model = model; viewData.Add("SiteName", "AldamarITB"); viewData.Add("HomeLink", "http://intranet.aldamaritb.com"); using (var writer = new StringWriter()) { var routeData = new RouteData(); routeData.Values.Add("controller", "test"); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); } }
public string RenderRazorViewToString(string viewName, object model) { ViewData.Model = model; using (var sw = new StringWriter()) { //var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, //viewName); //var viewContext = new ViewContext(ControllerContext, viewResult.View, //ViewData, TempData, sw); //viewResult.View.Render(viewContext, sw); //viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); //return sw.GetStringBuilder().ToString(); //var routeData = new RouteData(); //routeData.Values.Add("controller", "InvoiceView"); //ControllerContext controllerContext = new System.Web.Mvc.ControllerContext(); //controllerContext.RouteData = routeData; //var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, //viewName); //var viewContext = new ViewContext(ControllerContext, viewResult.View, //ViewData, TempData, sw); //viewResult.View.Render(viewContext, sw); //viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); //return sw.GetStringBuilder().ToString(); var routeData = new RouteData(); routeData.Values.Add("controller", "IndexView"); var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false); var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(ViewData), new TempDataDictionary(), sw); razorViewResult.View.Render(viewContext, sw); return(sw.ToString()); } }
public static string RenderViewToString(string controllerName, string viewName, object viewData, List <KeyValuePair <string, object> > additionalData) { var context = HttpContext.Current; var contextBase = new HttpContextWrapper(context); var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); var controllerContext = new ControllerContext(contextBase, routeData, new EmptyController()); var razorViewEngine = new RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false); ViewDataDictionary vdd = new ViewDataDictionary(viewData); if (additionalData != null && additionalData.Any()) { additionalData.ForEach(vdd.Add); } var writer = new StringWriter(); var viewContext = new ViewContext(controllerContext, razorViewResult.View, vdd, new TempDataDictionary(), writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); }
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { return(!IsExperienceEditorMode() ? NullViewEngineResult() : _viewEngine.FindView(controllerContext, GetExperienceEditorViewName(viewName), masterName, false)); }
public void FindView_ReturnsRazorView_IfLookupWasSuccessful() { // Arrange var pageFactory = new Mock<IRazorPageFactory>(); pageFactory.Setup(p => p.CreateInstance(It.IsAny<string>())) .Returns(Mock.Of<IRazorPage>()); var viewEngine = new RazorViewEngine(pageFactory.Object, Mock.Of<IRazorPageActivator>(), Mock.Of<IViewStartProvider>()); var context = GetActionContext(_controllerTestContext); // Act var result = viewEngine.FindView(context, "test-view"); // Assert Assert.True(result.Success); Assert.IsType<RazorView>(result.View); Assert.Equal("/Views/bar/test-view.cshtml", result.ViewName); }