public string Render(HttpContext context, string layout, string viewName, object model = null, Dictionary <string, object> viewData = null)
        {
            ActionContext actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());

            using (var sw = new StringWriter())
            {
                RazorPageResult viewResult = _razorViewEngine.FindPage(actionContext, viewName);


                viewResult.Page.Layout = layout;
                if (viewResult.Page == null)
                {
                    throw new Exception($"{viewName} does not match any available view");
                }

                var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());

                if (model != null)
                {
                    viewDictionary.Model = model;
                }

                if (viewData != null)
                {
                    foreach (var item in viewData)
                    {
                        viewDictionary[item.Key] = item.Value;
                    }
                }

                var opts = new HtmlHelperOptions();

                var viewContext = new ViewContext();
                //var viewContext = new ViewContext(
                //    actionContext,
                //    null,
                //    viewDictionary,
                //    new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
                //    sw,
                //    opts
                //);


                viewResult.Page.ViewContext = new ViewContext();
                viewResult.Page.ViewContext.View.RenderAsync(viewContext);

                Task.WaitAll(viewResult.Page.ExecuteAsync());
                viewResult.Page.ViewContext.ViewData = viewDictionary;
                viewResult.Page.ViewContext.Writer   = sw;
                Task.WaitAll(viewResult.Page.ViewContext.View.RenderAsync(viewContext));

                return(sw.ToString());
            }
        }
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (!(context is PageContext pageContext))
            {
                throw new ArgumentException($"{nameof(context)} should be instance of type {typeof(PageContext).Name}");
            }


            string executingFilePath = ".";
            string basePath          = this.Page.Path;
            string fileName          = Path.GetFileName(basePath);
            string pageName          = Path.GetFileNameWithoutExtension(basePath);
            string directory         = basePath.Substring(0, basePath.Length - fileName.Length);

            while (directory[directory.Length - 1] == '/')
            {
                directory = directory.Substring(0, directory.Length - 1);
            }

            HttpContext      httpContext     = pageContext.HttpContext;
            IRazorViewEngine engine          = httpContext.RequestServices.GetRequiredService <IRazorViewEngine>();
            ICultureContext  cultureContext  = httpContext.RequestServices.GetRequiredService <ICultureContext>();
            IFileCultureInfo fileCultureInfo = httpContext.RequestServices.GetRequiredService <ICultureFileCache>().Get(_requestedCulture, directory, pageName, "cshtml");

            if (fileCultureInfo != null)
            {
                httpContext.RequestServices.GetRequiredService <ILocalizedViewRenderContextAccessor>().Context = new LocalizedViewRenderContext(_requestedCulture, fileCultureInfo.Culture, cultureContext.UrlCultureSpecifier);
                string relativePath = fileCultureInfo.RelativePath; // 7 == ".cshtml".Length
                if (!relativePath.Equals(this.Page.Path))
                {
                    RazorPageResult pageResult        = engine.GetPage(executingFilePath, relativePath);
                    Page            page              = (Page)pageResult.Page;
                    PageContext     resultPageContext = pageContext.CreateCopy();
                    ViewContext     resultViewContext = this.Page.ViewContext.CreateCopy();
                    page.ViewContext = resultViewContext;
                    resultViewContext.ExecutingFilePath             = relativePath;
                    resultPageContext.ActionDescriptor.PageTypeInfo = page.GetType().GetTypeInfo();
                    resultPageContext.ActionDescriptor.RelativePath = relativePath;
                    page.PageContext = resultPageContext;
                    page.HtmlEncoder = this.Page.HtmlEncoder;
                    page.Path        = relativePath;
                    this.Page        = page;
                }
            }
            else
            {
                httpContext.RequestServices.GetRequiredService <ILocalizedViewRenderContextAccessor>().Context = new LocalizedViewRenderContext(_requestedCulture, null, cultureContext.UrlCultureSpecifier);
            }

            var executor = context.HttpContext.RequestServices.GetRequiredService <PageResultExecutor>();
            await executor.ExecuteAsync(pageContext, this);
        }
Example #3
0
        private ViewEngineResult CreateViewEngineResult(
            RazorPageResult result,
            IRazorViewFactory razorViewFactory,
            bool isPartial)
        {
            if (result.SearchedLocations != null)
            {
                return(ViewEngineResult.NotFound(result.Name, result.SearchedLocations));
            }

            var view = razorViewFactory.GetView(this, result.Page, isPartial);

            return(ViewEngineResult.Found(result.Name, view));
        }