Ejemplo n.º 1
0
        public async Task <IActionResult> View(int id)

        {
            var certificate = await _certificateRetrievalService
                              .GetCertificateByIdAsync(id,
                                                       CertificateRetrievalOptions.ForRendering);

            var html = await _certificateRenderer
                       .RenderToHtmlAsStringAsync(new CertificateViewModel(certificate));

            return(Content(html, MediaTypeNames.Text.Html));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get(int id, [FromQuery] CertificateFormat?format = null)
        {
            var cert = await _certificateRetrievalService
                       .GetCertificateByIdAsync(id, CertificateRetrievalOptions.ForRendering);

            format ??= GetCertificateFormatFromMediaType(Request.Headers[HeaderNames.Accept])
            ?? CertificateFormat.Json;

            switch (format)
            {
            case CertificateFormat.Json:
                return(Ok(new CertificateDto(cert)));

            case CertificateFormat.Html:
                var html = await _certificateRenderer
                           .RenderToHtmlAsStringAsync(new CertificateViewModel(cert));

                return(Content(html, MediaTypeNames.Text.Html));

            case CertificateFormat.Pdf:
                var stream = await _certificateRenderer
                             .RenderToPdfAsStreamAsync(new CertificateViewModel(cert));

                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);

                return(File(memoryStream.ToArray(), MediaTypeNames.Application.Pdf));

            default:
                throw new InvalidOperationException($"Unsupported cert format: {format}");
            }
        }