0

Each time, like trying to send an email, I will not be sent, but it says:

An unhandled exception occurred while processing the request. ArgumentNullException: Value cannot be null. Parameter name: Templates/NewPassword does not match any available view

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

That's how I've tried to look here.Github - Paris Plyzos

Also code here:

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 code here - I've written an error where the error goes wrong here.

 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 Answers1

1

How is your IViewRenderService.RenderToStringAsync() implementation finding views? If you are using IRazorViewEngine, the ViewName has to be fully qualified including file extension ex "~/Views/Home/Index.cshtml"

Neil
  • 1,613
  • 1
  • 16
  • 18
  • An unhandled exception occurred while processing the request. ArgumentNullException: Value cannot be null. Parameter name: ~/Views/Templates/NewPassword does not match any available view same error – J. Petersen Mar 06 '18 at 18:59
  • 1
    Did you include `.cshtml` at the end of your path? Or is your file actually just named `NewPassword` without any extension? I would assume the string you use should be `"~/Views/Templates/NewPassword.cshtml"` – Neil Mar 06 '18 at 19:16
  • Just noticed your `RenderToStringAsync()` function relies on `_razorViewEngine.FindView()`, mine uses `_razorViewEngine.GetView()` Try replacing your `viewResult` with this: `_razorViewEngine.GetView("", viewName, false);` – Neil Mar 06 '18 at 19:49
  • Then there is no error but questions are so from email if it should be my xxx@xxxx.com or if it should be smtp.sendgrid.net @Neil – J. Petersen Mar 06 '18 at 20:04
  • Is `RenderToStringAsync()` working correctly now? If so, mark this question as answered and open a new one with details about what the new issue is – Neil Mar 06 '18 at 20:40
  • It does not come up with errors anymore that it can not find the file or resemble it. So I'll shoot that it's really done now. @Neil – J. Petersen Mar 06 '18 at 20:42