public static async Task <string> RenderView(string path, ViewDataDictionary viewDataDictionary)
        {
            if (ServiceProvider == null)
            {
                throw new InvalidOperationException($"In order to use the facade, {nameof(UseMailTemplates)} must be called in Startup.Configure method. ");
            }

            path = path ?? throw new ArgumentNullException(nameof(path));
            viewDataDictionary = viewDataDictionary ?? throw new ArgumentNullException(nameof(viewDataDictionary));

            path = path.EndsWith(".cshtml") ? path : $"{path}.cshtml";
            path = path.Trim('/');

            using (IServiceScope scope = ServiceProvider.CreateScope())
            {
                MailOptions          options             = scope.ServiceProvider.GetRequiredService <IOptions <MailOptions> >().Value;
                ICompositeViewEngine viewEngine          = scope.ServiceProvider.GetRequiredService <ICompositeViewEngine>();
                ITempDataProvider    tempDataProvider    = scope.ServiceProvider.GetRequiredService <ITempDataProvider>();
                IHttpContextAccessor httpContextAccessor = scope.ServiceProvider.GetRequiredService <IHttpContextAccessor>();
                HttpContext          httpContext         = httpContextAccessor.HttpContext;
                if (httpContext == null)
                {
                    httpContext = new DefaultHttpContext
                    {
                        RequestServices = scope.ServiceProvider
                    };
                }
                ActionContext actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
                viewDataDictionary.Model = null;

                using (StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = viewEngine.GetView($"{options.ViewTemplateBasePath}/{path}", $"{options.ViewTemplateBasePath}/{path}", true);

                    if (viewResult?.View == null)
                    {
                        throw new Exception($"View {options.ViewTemplateBasePath}/{path} not found.");
                    }

                    ViewContext viewContext = new ViewContext(actionContext, viewResult.View, viewDataDictionary, new TempDataDictionary(httpContext, tempDataProvider), sw, new HtmlHelperOptions());

                    await viewResult.View.RenderAsync(viewContext);

                    sw.Flush();

                    if (viewContext.ViewData != viewDataDictionary)
                    {
                        var keys = viewContext.ViewData.Keys.ToArray();
                        foreach (var key in keys)
                        {
                            viewDataDictionary[key] = viewContext.ViewData[key];
                        }
                    }

                    return(sw.ToString());
                }
            }
        }
Exemple #2
0
 public EmailSender(IOptions <MailOptions> options)
 {
     Options = options.Value;
 }