Пример #1
0
        public static string ExcuteToStringAsync(this ViewExecutor viewExecutor,
                                                 ActionContext actionContext,
                                                 IView view,
                                                 ViewDataDictionary viewData,
                                                 ITempDataDictionary tempData,
                                                 string contentType,
                                                 int?statusCode)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }

            var services = actionContext.HttpContext.RequestServices;

            var viewOption = services.GetRequiredService <IOptions <MvcViewOptions> >();

            if (viewData == null)
            {
                var metadataProvider = services.GetRequiredService <IModelMetadataProvider>();
                viewData = new ViewDataDictionary(metadataProvider, actionContext.ModelState);
            }

            if (tempData == null)
            {
                tempData = services.GetRequiredService <ITempDataDictionary>();
            }

            var response = actionContext.HttpContext.Response;

            string   resolvedContentType         = null;
            Encoding resolvedContentTypeEncoding = null;

            ResolveContentTypeAndEncoding(
                contentType,
                response.ContentType,
                DefaultContentType,
                out resolvedContentType,
                out resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;

            if (statusCode != null)
            {
                response.StatusCode = statusCode.Value;
            }


            using (var writer = new StringWriter())
            {
                var viewContext = new ViewContext(
                    actionContext,
                    view,
                    viewData,
                    tempData,
                    writer,
                    viewOption.Value.HtmlHelperOptions);

                //viewExecutor.DiagnosticSource.BeforeView(view, viewContext);

                view.RenderAsync(viewContext);

                //DiagnosticSource.AfterView(view, viewContext);

                // Perf: Invoke FlushAsync to ensure any buffered content is asynchronously written to the underlying
                // response asynchronously. In the absence of this line, the buffer gets synchronously written to the
                // response as part of the Dispose which has a perf impact.
                writer.FlushAsync();

                return(writer.GetStringBuilder().ToString());
            }
        }
Пример #2
0
        public static IHtmlContent ExecuteToHTML(this ViewExecutor viewExecutor,
                                                 ActionContext actionContext,
                                                 IView view,
                                                 object Model,
                                                 ViewDataDictionary viewData,
                                                 ITempDataDictionary tempData,
                                                 string contentType,
                                                 int?statusCode)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }

            var services = actionContext.HttpContext.RequestServices;

            var viewOption = services.GetRequiredService <IOptions <MvcViewOptions> >();
            var executor   = services.GetRequiredService <ViewResultExecutor>();
            var htmlHelper = services.GetRequiredService <IHtmlHelper>();

            if (viewData == null)
            {
                var metadataProvider = services.GetRequiredService <IModelMetadataProvider>();
                viewData = new ViewDataDictionary(metadataProvider, actionContext.ModelState);
            }

            if (tempData == null)
            {
                tempData = services.GetRequiredService <ITempDataDictionary>();
            }

            var response = actionContext.HttpContext.Response;

            string   resolvedContentType         = null;
            Encoding resolvedContentTypeEncoding = null;

            ResolveContentTypeAndEncoding(
                contentType,
                response.ContentType,
                DefaultContentType,
                out resolvedContentType,
                out resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;

            if (statusCode != null)
            {
                response.StatusCode = statusCode.Value;
            }

            using (var writer = new StringWriter())
            {
                //var viewContext = new ViewContext(
                //    actionContext,
                //    view,
                //    viewData,
                //    tempData,
                //    writer,
                //    viewOption.Value.HtmlHelperOptions);

                var routeData = new RouteData();
                //routeData.Values.Add("area", actionContext.RouteData.Values["area"]); //area information has been removed intentionally, because this route date spoils the view context of the parent (Page Controller)
                routeData.Values.Add("controller", actionContext.RouteData.Values["controller"]);
                routeData.Values.Add("action", actionContext.RouteData.Values["action"]);
                routeData.Values.Add("pageModuleId", actionContext.RouteData.Values["pageModuleId"]);
                foreach (var rt in actionContext.RouteData.Routers)
                {
                    routeData.Routers.Add(rt);
                }

                var ac = new ActionContext(actionContext.HttpContext, routeData, actionContext.ActionDescriptor);

                var viewContext = new ViewContext(
                    ac,
                    view,
                    viewData,
                    tempData,
                    writer,
                    viewOption.Value.HtmlHelperOptions);

                ((HtmlHelper)htmlHelper).Contextualize(viewContext);

                var result = htmlHelper.Partial(view.Path, Model, viewData);

                writer.FlushAsync();

                return(result);
            }
        }