Convert Razor View to string ASP.NET core : <p>I want to send a view as email body, i'm using sendgrid.
How do i convert view to string ?</p>

<p>I got a code here <a href="https://long2know.com/2017/08/rendering-and-emailing-embedded-razor-views-with-net-core/" rel="nofollow noreferrer">https://long2know.com/2017/08/rendering-and-emailing-embedded-razor-views-with-net-core/</a></p>

<pre><code>
public string RenderToString&lt;TModel&gt;(string viewPath, TModel model)
        {
            try
            {
                var viewEngineResult = _viewEngine.GetView("Views/", viewPath, false);

                if (!viewEngineResult.Success)
                {
                    throw new InvalidOperationException($"Couldn't find view {viewPath}");
                }

                var view = viewEngineResult.View;

                using (var sw = new StringWriter())
                {
                    var viewContext = new ViewContext()
                    {
                        HttpContext = _httpContextAccessor.HttpContext ?? new DefaultHttpContext { RequestServices = _serviceProvider },
                        ViewData = new ViewDataDictionary&lt;TModel&gt;(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model },
                        Writer = sw
                    };
                    view.RenderAsync(viewContext).GetAwaiter().GetResult();
                    return sw.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error ending email.", ex);
            }
        }


</code></pre>

<p>This line say nullreference error</p>

<pre><code>view.RenderAsync(viewContext).GetAwaiter().GetResult();
</code></pre>
