示例#1
0
        static string RenderPartialRazorView(string viewPath, object model = null)
        {
            using (var writer = new StringWriter())
            {
                var controllerContext = new ControllerContext();

                // Выборка директории, в которой находится вьюха
                var viewDir = Regex.Match(viewPath, @"(?<=~(/bin)?/Views/)\w+(?=/.*)").Value;
                controllerContext.RouteData.Values.Add("controller", viewDir == "Shared" ? "Home" : viewDir);
                var httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
                controllerContext.RequestContext.HttpContext = httpContextWrapper;
                controllerContext.HttpContext = httpContextWrapper;
                RoViewEngine razorViewEngine = ViewEngines.Engines.OfType <RoViewEngine>().First();
                var          viewData        = new ViewDataDictionary();
                if (model != null)
                {
                    viewData.Model = model;
                }
                ViewEngineResult viewResult = razorViewEngine.FindPartialView(controllerContext, viewPath, false);
                var viewContext             = new ViewContext(controllerContext, viewResult.View,
                                                              viewData, new TempDataDictionary(), writer);
                viewResult.View.Render(viewContext, writer);
                return(writer.ToString());
            }
        }
示例#2
0
        public string RenderEmailMessage(string template, object model = null)
        {
            using (var writer = new StringWriter())
            {
                var controllerContext = new ControllerContext();
                controllerContext.RouteData.Values.Add("controller", "Message");

                string viewPath = String.Format("_{0}", template);

                HttpContext httpContext        = null; //данная переменная получает значение только в случае, если HttpContext.Current == null
                var         httpContextWrapper = new HttpContextWrapper(HttpContext.Current ??
                                                                        (httpContext = new HttpContext(new SimpleWorkerRequest("", "", new StringWriter()))));
                controllerContext.RequestContext.HttpContext = httpContextWrapper;
                controllerContext.HttpContext = httpContextWrapper;
                RoViewEngine razorViewEngine = ViewEngines.Engines.OfType <RoViewEngine>().First();
                var          viewData        = new ViewDataDictionary();
                if (model != null)
                {
                    viewData.Model = model;
                }
                ViewEngineResult viewResult = null;
                viewResult = razorViewEngine.FindPartialView(controllerContext, viewPath, false);
                var viewContext = new ViewContext(controllerContext, viewResult.View,
                                                  viewData, new TempDataDictionary(), writer);

                if (httpContext != null)
                {
                    HttpContext.Current = httpContext;
                }

                viewResult.View.Render(viewContext, writer);
                return(writer.ToString());
            }
        }