fixing possible NRE, when viewContext is null

This commit is contained in:
sprelog 2019-03-08 18:48:18 +01:00 committed by Pranav K
parent f4c80ca99f
commit bc009fc0f1
1 changed files with 5 additions and 3 deletions

View File

@ -644,9 +644,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor
public virtual HtmlString SetAntiforgeryCookieAndHeader()
{
var viewContext = ViewContext;
var antiforgery = viewContext?.HttpContext.RequestServices.GetRequiredService<IAntiforgery>();
antiforgery.SetCookieTokenAndHeader(viewContext?.HttpContext);
if (viewContext != null)
{
var antiforgery = viewContext.HttpContext.RequestServices.GetRequiredService<IAntiforgery>();
antiforgery.SetCookieTokenAndHeader(viewContext.HttpContext);
}
return HtmlString.Empty;
}