< Summary

Class:SVETA.Api.Helpers.TemplateHelper
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Helpers/TemplateHelper.cs
Covered lines:0
Uncovered lines:38
Coverable lines:38
Total lines:74
Line coverage:0% (0 of 38)
Covered branches:0
Total branches:4
Branch coverage:0% (0 of 4)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetTemplateHtmlAsStringAsync()0%0%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Helpers/TemplateHelper.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Text;
 4using System.Threading.Tasks;
 5using Microsoft.AspNetCore.Http;
 6using Microsoft.AspNetCore.Mvc;
 7using Microsoft.AspNetCore.Mvc.Abstractions;
 8using Microsoft.AspNetCore.Mvc.ModelBinding;
 9using Microsoft.AspNetCore.Mvc.Razor;
 10using Microsoft.AspNetCore.Mvc.Rendering;
 11using Microsoft.AspNetCore.Mvc.ViewFeatures;
 12using Microsoft.AspNetCore.Routing;
 13
 14namespace SVETA.Api.Helpers
 15{
 16    public interface ITemplateHelper
 17    {
 18        Task<string> GetTemplateHtmlAsStringAsync<T>(string viewName, T model);
 19    }
 20
 21    public class TemplateHelper : ITemplateHelper
 22    {
 23        private IRazorViewEngine _razorViewEngine;
 24        private IServiceProvider _serviceProvider;
 25        private ITempDataProvider _tempDataProvider;
 26
 027        public TemplateHelper(
 028            IRazorViewEngine engine,
 029            IServiceProvider serviceProvider,
 030            ITempDataProvider tempDataProvider)
 031        {
 032            this._razorViewEngine = engine;
 033            this._serviceProvider = serviceProvider;
 034            this._tempDataProvider = tempDataProvider;
 035        }
 36
 37        public async Task<string> GetTemplateHtmlAsStringAsync<T>(string viewName, T model)
 038        {
 039            var httpContext = new DefaultHttpContext() { RequestServices = _serviceProvider };
 040            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
 041            var stringBuilder = new StringBuilder();
 042            using (ExtentedStringWriter sw = new ExtentedStringWriter(stringBuilder, Encoding.UTF8))
 043            {
 044                var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
 45
 046                if (viewResult.View == null)
 047                {
 048                    return string.Empty;
 49                }
 50
 051                var viewDataDictionary = new ViewDataDictionary(
 052                    new EmptyModelMetadataProvider(),
 053                    new ModelStateDictionary()
 054                )
 055                {
 056                    Model = model
 057                };
 58
 059                var viewContext = new ViewContext(
 060                    actionContext,
 061                    viewResult.View,
 062                    viewDataDictionary,
 063                    new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
 064                    sw,
 065                    new HtmlHelperOptions()
 066                );
 67
 068                await viewResult.View.RenderAsync(viewContext);
 069            }
 070            return stringBuilder.ToString();
 071        }
 72    }
 73
 74}