Exemple #1
0
        /// <summary>
        /// Executes the <see cref="IView"/> asynchronously.
        /// </summary>
        /// <param name="actionContext">The <see cref="ActionContext"/> associated with the current request.</param>
        /// <param name="view">The <see cref="IView"/>.</param>
        /// <param name="viewResult">The <see cref="ViewResult"/>.</param>
        /// <returns>A <see cref="Task"/> which will complete when view execution is completed.</returns>
        public async Task ExecuteAsync(ActionContext actionContext, IView view, PdfResult viewResult)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

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

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

            var response = actionContext.HttpContext.Response;

            response.Headers.Add("Content-Disposition", viewResult.ContentDisposition);


            string   resolvedContentType         = null;
            Encoding resolvedContentTypeEncoding = null;

            ResponseContentTypeHelper.ResolveContentTypeAndEncoding(viewResult.ContentType, viewResult.ContentType, DefaultContentType, out resolvedContentType, out resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;
            response.StatusCode  = viewResult.StatusCode;

            var razorStream = new MemoryStream();

            using (var writer = new StreamWriter(razorStream))
            {
                var viewContext = new ViewContext(
                    actionContext,
                    view,
                    viewResult.ViewData,
                    viewResult.TempData,
                    writer,
                    ViewOptions.HtmlHelperOptions);

                await view.RenderAsync(viewContext);

                await writer.FlushAsync();

                Document document = new Document(viewResult.PageSize);
                // step 2
                var pdfWriter = PdfWriter.GetInstance(document, response.Body);

                razorStream.Position = 0;
                using (var tr = new StreamReader(razorStream))
                {
                    // step 3
                    document.Open();
                    XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, tr);
                    document.Close();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Attempts to find the <see cref="IView"/> associated with <paramref name="viewResult"/>.
        /// </summary>
        /// <param name="actionContext">The <see cref="ActionContext"/> associated with the current request.</param>
        /// <param name="viewResult">The <see cref="ViewResult"/>.</param>
        /// <returns>A <see cref="ViewEngineResult"/>.</returns>
        public virtual ViewEngineResult FindView(ActionContext actionContext, PdfResult viewResult)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }

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

            var viewEngine = viewResult.ViewEngine ?? ViewEngine;
            var viewName   = viewResult.ViewName ?? GetActionName(actionContext);

            var result         = viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: false);
            var originalResult = result;

            if (!result.Success)
            {
                result = viewEngine.FindView(actionContext, viewName, isMainPage: false);
            }

            if (!result.Success)
            {
                if (originalResult.SearchedLocations.Any())
                {
                    if (result.SearchedLocations.Any())
                    {
                        // Return a new ViewEngineResult listing all searched locations.
                        var locations = new List <string>(originalResult.SearchedLocations);
                        locations.AddRange(result.SearchedLocations);
                        result = ViewEngineResult.NotFound(viewName, locations);
                    }
                    else
                    {
                        // GetView() searched locations but FindView() did not. Use first ViewEngineResult.
                        result = originalResult;
                    }
                }
            }

            if (result.Success)
            {
                if (DiagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.ViewFound"))
                {
                    DiagnosticSource.Write(
                        "Microsoft.AspNetCore.Mvc.ViewFound",
                        new
                    {
                        actionContext = actionContext,
                        isMainPage    = false,
                        result        = viewResult,
                        viewName      = viewName,
                        view          = result.View,
                    });
                }
            }
            else
            {
                if (DiagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.ViewNotFound"))
                {
                    DiagnosticSource.Write(
                        "Microsoft.AspNetCore.Mvc.ViewNotFound",
                        new
                    {
                        actionContext     = actionContext,
                        isMainPage        = false,
                        result            = viewResult,
                        viewName          = viewName,
                        searchedLocations = result.SearchedLocations
                    });
                }
            }

            return(result);
        }