-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRazorViewToStringRenderer.cs
88 lines (77 loc) · 3.67 KB
/
RazorViewToStringRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace AzureFunctionsRazorEmailTemplateSample
{
public class RazorViewToStringRenderer
{
private readonly IRazorViewEngine viewEngine;
private readonly ITempDataProvider tempDataProvider;
private readonly IServiceProvider serviceProvider;
public RazorViewToStringRenderer(IRazorViewEngine viewEngine, ITempDataProvider tempDataProvider, IServiceProvider serviceProvider)
{
this.viewEngine = viewEngine;
this.tempDataProvider = tempDataProvider;
this.serviceProvider = serviceProvider;
}
/// <summary>
/// Renders a Razor view and model to a string.
/// The name of the view is automaticaly inferred from the model name (e.g. IndexModel -> Index).
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public async Task<string> RenderViewToStringAsync<TModel>(TModel model)
{
var templateName = typeof(TModel).Name.Replace("Model", "");
return await RenderViewToStringAsync(model, templateName);
}
/// <summary>
/// Renders a Razor view and model to a string.
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public async Task<string> RenderViewToStringAsync<TModel>(TModel model, string viewName)
{
var actionContext = GetActionContext();
var view = FindView(actionContext, viewName);
using var output = new StringWriter();
var viewDataDictionary = new ViewDataDictionary<TModel>(
metadataProvider: new EmptyModelMetadataProvider(), modelState: new ModelStateDictionary())
{
Model = model
};
var tempDataDictionary = new TempDataDictionary(actionContext.HttpContext, tempDataProvider);
var viewContext = new ViewContext(actionContext, view, viewDataDictionary, tempDataDictionary, output, new HtmlHelperOptions());
await view.RenderAsync(viewContext);
return output.ToString();
}
private IView FindView(ActionContext actionContext, string viewName)
{
var getViewResult = viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
if (getViewResult.Success) return getViewResult.View;
var findViewResult = viewEngine.FindView(actionContext, viewName, isMainPage: true);
if (findViewResult.Success) return findViewResult.View;
var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
var errorMessage = string.Join(Environment.NewLine,
new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));
throw new InvalidOperationException(errorMessage);
}
private ActionContext GetActionContext()
{
var httpContext = new DefaultHttpContext { RequestServices = serviceProvider };
return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
}
}
}