< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.Utilities.PrintPDF.ViewAsPdf
Assembly: ZhEaIsNsAaBn.Utilities.PrintPDF
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.PrintPDF/ViewAsPdf.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 123
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
GetHtmlAsStringAsync()0%4260%
GetView(...)0%2040%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.PrintPDF/ViewAsPdf.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc.Controllers;
 2using Microsoft.AspNetCore.Mvc.ModelBinding;
 3using Microsoft.AspNetCore.Mvc.Rendering;
 4using Microsoft.AspNetCore.Mvc.ViewEngines;
 5using Microsoft.AspNetCore.Mvc.ViewFeatures;
 6using Microsoft.Extensions.DependencyInjection;
 7
 8namespace ZhEaIsNsAaBn.Utilities.PrintPDF;
 9
 10/// <summary>A Razor view (and model) to print with <see cref="ChromiumHandler.PrintPdfFromViewAsync"/>.</summary>
 11public class ViewAsPdf : ViewResult
 12{
 13    /// <summary>Creates a view to print; the view name defaults to the action name.</summary>
 14    /// <param name="viewData">Extra view data.</param>
 015    public ViewAsPdf( ViewDataDictionary? viewData = null )
 16    {
 017        MasterName = string.Empty;
 018        ViewName   = string.Empty;
 019        Model      = null;
 020        ViewData   = viewData;
 021    }
 22
 23    /// <summary>Creates a view to print.</summary>
 24    /// <param name="viewName">The view name or path.</param>
 25    /// <param name="model">The model.</param>
 26    /// <param name="viewData">Extra view data.</param>
 027    public ViewAsPdf( string viewName, object? model, ViewDataDictionary? viewData = null ) : this( viewData )
 28    {
 029        ViewName = viewName;
 030        Model    = model;
 031    }
 32
 33    /// <summary>Creates a view to print.</summary>
 34    /// <param name="viewName">The view name or path.</param>
 35    /// <param name="viewData">Extra view data.</param>
 036    public ViewAsPdf( string viewName, ViewDataDictionary? viewData = null ) : this( viewData ) => ViewName = viewName;
 37
 38    /// <summary>Creates a view to print, named after the action.</summary>
 39    /// <param name="model">The model.</param>
 40    /// <param name="viewData">Extra view data.</param>
 041    public ViewAsPdf( object? model, ViewDataDictionary? viewData = null ) : this( viewData ) => Model = model;
 42
 43    /// <summary>Creates a view to print with a layout.</summary>
 44    /// <param name="viewName">The view name or path.</param>
 45    /// <param name="masterName">The layout name (kept for callers; the view's own layout is used).</param>
 46    /// <param name="model">The model.</param>
 047    public ViewAsPdf( string viewName, string masterName, object? model ) : this( viewName, model ) => MasterName = mast
 48
 49
 50    /// <summary>The view name or path; empty means the action name.</summary>
 51    public new string ViewName { get; init; } = string.Empty;
 52
 53    /// <summary>The layout name.</summary>
 54    public string MasterName { get; init; } = string.Empty;
 55
 56    /// <summary>The model.</summary>
 57    public new object? Model { get; init; }
 58
 59    /// <summary>Extra view data copied into the render.</summary>
 60    public new ViewDataDictionary? ViewData { get; set; }
 61
 62
 63    /// <summary>Renders the view to an HTML string.</summary>
 64    /// <param name="context">The action context.</param>
 65    /// <returns>The HTML.</returns>
 66    /// <exception cref="InvalidOperationException">The view can't be found.</exception>
 67    protected internal virtual async Task<string> GetHtmlAsStringAsync( ActionContext context )
 68    {
 069        ArgumentNullException.ThrowIfNull( context );
 70
 071        var viewName = string.IsNullOrEmpty( ViewName ) ? ( (ControllerActionDescriptor) context.ActionDescriptor ).Acti
 072        var view     = GetView( context, viewName, MasterName ).View!;
 73
 74
 075        var tempDataProvider   = context.HttpContext.RequestServices.GetService<ITempDataProvider>();
 076        var viewDataDictionary = new ViewDataDictionary( new EmptyModelMetadataProvider(), new ModelStateDictionary() ) 
 77
 078        if ( ViewData is not null )
 79        {
 080            foreach ( var item in ViewData ) { viewDataDictionary.Add( item ); }
 81        }
 82
 83
 084        using var output             = new StringWriter();
 085        var       tempDataDictionary = new TempDataDictionary( context.HttpContext, tempDataProvider! );
 086        var       viewContext        = new ViewContext( context, view, viewDataDictionary, tempDataDictionary, output, n
 87
 088        await view.RenderAsync( viewContext ).ConfigureAwait( false );
 89
 90
 091        return output.ToString();
 092    }
 93
 94    /// <summary>Finds the view by path, then by name.</summary>
 95    /// <param name="context">The action context.</param>
 96    /// <param name="viewName">The view name or path.</param>
 97    /// <param name="masterName">The layout name.</param>
 98    /// <returns>The found view.</returns>
 99    /// <exception cref="InvalidOperationException">The view can't be found; the message lists the searched locations.</
 100    protected virtual ViewEngineResult GetView( ActionContext context, string viewName, string masterName )
 101    {
 0102        ArgumentNullException.ThrowIfNull( context );
 103
 0104        var engine = context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
 105
 106
 0107        var getViewResult = engine.GetView( null, viewName, true );
 108
 0109        if ( getViewResult.Success ) { return getViewResult; }
 110
 111
 0112        var findViewResult = engine.FindView( context, viewName, true );
 113
 0114        if ( findViewResult.Success ) { return findViewResult; }
 115
 116
 0117        var searchedLocations = getViewResult.SearchedLocations.Concat( findViewResult.SearchedLocations );
 118
 119
 0120        throw new InvalidOperationException( string.Join( Environment.NewLine,
 0121                                                          new[] { $"Unable to find view '{viewName}'. The following loca
 122    }
 123}