我的电子邮件模板似乎为空。 - 在RenderToStringAsync上给出错误

时间:2018-03-06 17:13:05

标签: c# asp.net-core-2.0

每次,就像尝试发送电子邮件一样,我不会被发送,但它会说:

  

处理请求时发生未处理的异常。   ArgumentNullException:值不能为null。参数名称:   Templates / NewPassword与任何可用视图都不匹配

This is what it looks like when I refer to the file.

我就是这样看的。Github - Paris Plyzos

也在这里编码:

var resultMail = await _viewRenderService.RenderToStringAsync("Templates/NewPassword", viewModel); //ERROR HERE!

var client = new SendGridClient(m.azureName());
var from = new EmailAddress(m.mailFrom(), m.nameFrom());
var to = new EmailAddress(mail, UserValue.Navn);
var plainTextContent = Regex.Replace(resultMail, "<[^>]*>", "");
var msg = MailHelper.CreateSingleEmail(from, to, title, plainTextContent: plainTextContent,
                        htmlContent: null);
var resulta = client.SendEmailAsync(msg);

return RedirectToAction("UserPassword");

这里的RenderToStringAsync代码 - 我写了一个错误,其中错误在这里出错。

 public async Task<string> RenderToStringAsync(string viewName, object model)
    {
        var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

        using (var sw = new StringWriter())
        {
            var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);//ERROR HERE

            if (viewResult.View == null)
            {
                throw new ArgumentNullException($"{viewName} does not match any available view");
            }

            var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = model
            };

            var viewContext = new ViewContext(actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
                sw,
                new HtmlHelperOptions()
            );

            await viewResult.View.RenderAsync(viewContext);
            return sw.ToString();
        }
    }

1 个答案:

答案 0 :(得分:0)

您的IViewRenderService.RenderToStringAsync()实施查找视图的方式如何?如果您使用的是IRazorViewEngine,则ViewName必须是完全限定的,包括文件扩展名为"~/Views/Home/Index.cshtml"