예제 #1
0
        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());
        }
예제 #2
0
        public static string RenderPartial(string partialName, object model, string[] viewLocations)
        {
            using (var sw = new StringWriter())
            {
                var httpContext = new HttpContextWrapper(HttpContext.Current);

                var routeData = new RouteData();
                routeData.Values.Add("controller", "DummyController");

                var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new DummyController());

                var viewEngine = new RazorViewEngine
                {
                    PartialViewLocationFormats = viewLocations
                };

                var viewResult = viewEngine.FindPartialView(controllerContext, partialName, false);
                if (viewResult.View == null)
                {
                    // TODO: Log lack of view?
                    return(null);
                }

                viewResult.View.Render(new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary {
                    Model = model
                }, new TempDataDictionary(), sw), sw);

                return(sw.ToString());
            }
        }
        internal static string RenderPartial(string partialName, object model)
        {
            using (var sw = new StringWriter())
            {
                var httpContext = new HttpContextWrapper(HttpContext.Current);

                var routeData = new RouteData();
                routeData.Values.Add("controller", "DummyController");

                var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new DummyController());

                var viewResult = ViewEngine.FindPartialView(controllerContext, partialName, false);
                if (viewResult.View == null)
                {
                    LogHelper.Warn(typeof(ViewHelper), $"No view found for partial '{partialName}'");
                    return(null);
                }

                viewResult.View.Render(new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary {
                    Model = model
                }, new TempDataDictionary(), sw), sw);

                return(sw.ToString());
            }
        }
예제 #4
0
        public static string RenderPartialViewToString <T>(string viewName, T model, string controllerName, HttpRequestMessage request)
        {
            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, request.RequestUri.ToString(), null), new HttpResponse(null))), routeData, new HelpController(null));

                var razorViewEngine = new RazorViewEngine();
                var razorViewResult = razorViewEngine.FindPartialView(fakeControllerContext, viewName, false);

                var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), writer);
                razorViewResult.View.Render(viewContext, writer);
                return(writer.ToString());
            }
        }
예제 #5
0
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            var viewModel = controllerContext.Controller.ViewData.Model;

            if (controllerContext is ViewContext)
            {
                viewModel = ((ViewContext)controllerContext).ViewData.Model;
            }
            partialViewName = TryFindViewFromViewModel(controllerContext.HttpContext.Cache, viewModel) ?? partialViewName;
            var result = base.FindPartialView(controllerContext, partialViewName, useCache);

            if (result.View == null && !PreemptPhysicalFiles)
            {
                result = razorViewEngine.FindPartialView(controllerContext, partialViewName, useCache);
            }
            return(result);
        }
        internal static string RenderPartial(string partialName, ViewDataDictionary viewData)
        {
            using (var sw = new StringWriter())
            {
                var httpContext = new HttpContextWrapper(HttpContext.Current);

                var routeData = new RouteData {
                    Values = { { "controller", nameof(ContentBlocksFakeController) } }
                };

                var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new ContentBlocksFakeController());

                var viewResult = _viewEngine.FindPartialView(controllerContext, partialName, false);

                if (viewResult.View == null)
                {
                    return(null);
                }

                viewResult.View.Render(new ViewContext(controllerContext, viewResult.View, viewData, new TempDataDictionary(), sw), sw);

                return(sw.ToString());
            }
        }
 public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
 {
     return(!IsExperienceEditorMode() ? NullViewEngineResult() :
            _viewEngine.FindPartialView(controllerContext, GetExperienceEditorViewName(partialViewName), false));
 }