Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
That's when, for example, I'm getting a user on the page. and sends a kind of welcome email to person. then it tells me:

ArgumentNullException: Value cannot be null. Parameter name: Email/NewPassword does not match any available view

It is the case that my registered user is in Account and when I send mail, it is in a folder at Views/Email/NewPassword.

I have used the information contained here.


When I try to "call" the area that I would like to get hold of, I'll do it here. And This piece of code I use in my account controller.

C#
var resultMail = await _viewRenderService.RenderToStringAsync("Email/NewPassword", viewModel);
var client = new SendGridClient(m.AzureName());
var from = new EmailAddress(m.MailFrom(), m.NameFromUsername());
var to = new EmailAddress(mail, UserValue.Navn);
var plainTextContent = Regex.Replace(resultMail, "<[^>]*>", "");
var msg = MailHelper.CreateSingleEmail(from, to, title, plainTextContent: plainTextContent,
var resulta = client.SendEmailAsync(msg);


In with - RenderToStringAsync

Where errors like happens there is where I have written "the error is here".

C#
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);

            //the error is 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();
        }
    }


What I have tried:

I've tried a part and I've tried that with GetView so you get the content I want to grab.
Posted
Updated 10-Apr-18 3:14am
Comments
Richard Deeming 2-Mar-18 11:51am    
Is the file called NewPassword.cshtml in the folder Views/Email, or is it in a folder called Views/Email/NewPassword?

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

And for some reason, viewResult is null. Why? Because the call the FindView returned null. Why? Don't know - you need to look at the parameters you pass it and see what they are, and why that might cause the view not to be found.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Paul Bester 10-Apr-18 9:09am    
Most ridiculous answer I've seen in a while. If you have tried to google 2 seconds, you would've seen, this is an actual issue with .net core. I assume he's not stupid and knows what gives him a null (going through the exact same issue, without success yet). The issue here is, that it shouldn't return a null, but it does. As this is working in MVC 5 without issue. From what I understand, its how the paths are parsed or something in the FindView function.
Currently, I cant find a proper solution to this, except for a "workaround" that was suggested.

Pass in the full "view" path of the file to RenderToStringAsync and in that function, instead of using FindView, use GetView For example:

RenderToStringAsync:
viewResult = _razorViewEngine.GetView(_env.WebRootPath, viewName, false);


Then in your controller:
var resultMail = await _viewRenderService.RenderToStringAsync("/Views/Email/NewPassword.cshtml", viewModel);


Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900