| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.AspNetCore.Mvc.Abstractions; |
| | | 8 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | | 9 | | using Microsoft.AspNetCore.Mvc.Razor; |
| | | 10 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 11 | | using Microsoft.AspNetCore.Mvc.ViewFeatures; |
| | | 12 | | using Microsoft.AspNetCore.Routing; |
| | | 13 | | |
| | | 14 | | namespace 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 | | |
| | 0 | 27 | | public TemplateHelper( |
| | 0 | 28 | | IRazorViewEngine engine, |
| | 0 | 29 | | IServiceProvider serviceProvider, |
| | 0 | 30 | | ITempDataProvider tempDataProvider) |
| | 0 | 31 | | { |
| | 0 | 32 | | this._razorViewEngine = engine; |
| | 0 | 33 | | this._serviceProvider = serviceProvider; |
| | 0 | 34 | | this._tempDataProvider = tempDataProvider; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<string> GetTemplateHtmlAsStringAsync<T>(string viewName, T model) |
| | 0 | 38 | | { |
| | 0 | 39 | | var httpContext = new DefaultHttpContext() { RequestServices = _serviceProvider }; |
| | 0 | 40 | | var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); |
| | 0 | 41 | | var stringBuilder = new StringBuilder(); |
| | 0 | 42 | | using (ExtentedStringWriter sw = new ExtentedStringWriter(stringBuilder, Encoding.UTF8)) |
| | 0 | 43 | | { |
| | 0 | 44 | | var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); |
| | | 45 | | |
| | 0 | 46 | | if (viewResult.View == null) |
| | 0 | 47 | | { |
| | 0 | 48 | | return string.Empty; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | var viewDataDictionary = new ViewDataDictionary( |
| | 0 | 52 | | new EmptyModelMetadataProvider(), |
| | 0 | 53 | | new ModelStateDictionary() |
| | 0 | 54 | | ) |
| | 0 | 55 | | { |
| | 0 | 56 | | Model = model |
| | 0 | 57 | | }; |
| | | 58 | | |
| | 0 | 59 | | var viewContext = new ViewContext( |
| | 0 | 60 | | actionContext, |
| | 0 | 61 | | viewResult.View, |
| | 0 | 62 | | viewDataDictionary, |
| | 0 | 63 | | new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), |
| | 0 | 64 | | sw, |
| | 0 | 65 | | new HtmlHelperOptions() |
| | 0 | 66 | | ); |
| | | 67 | | |
| | 0 | 68 | | await viewResult.View.RenderAsync(viewContext); |
| | 0 | 69 | | } |
| | 0 | 70 | | return stringBuilder.ToString(); |
| | 0 | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | } |