Resolve virtual ViewContext max once per method

This commit is contained in:
Ben Adams 2018-08-28 03:08:55 +01:00 committed by Pranav K
parent f90a47c5af
commit 3dfa26f7e3
2 changed files with 29 additions and 21 deletions

View File

@ -196,11 +196,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor
else if (required) else if (required)
{ {
// If the section is not found, and it is not optional, throw an error. // If the section is not found, and it is not optional, throw an error.
var message = Resources.FormatSectionNotDefined( var viewContext = ViewContext;
ViewContext.ExecutingFilePath, throw new InvalidOperationException(
sectionName, Resources.FormatSectionNotDefined(
ViewContext.View.Path); viewContext.ExecutingFilePath,
throw new InvalidOperationException(message); sectionName,
viewContext.View.Path));
} }
else else
{ {

View File

@ -51,13 +51,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor
{ {
get get
{ {
if (ViewContext == null) var viewContext = ViewContext;
if (viewContext == null)
{ {
var message = Resources.FormatViewContextMustBeSet("ViewContext", "Output"); throw new InvalidOperationException(Resources.FormatViewContextMustBeSet(nameof(ViewContext), nameof(Output)));
throw new InvalidOperationException(message);
} }
return ViewContext.Writer; return viewContext.Writer;
} }
} }
@ -183,8 +183,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </remarks> /// </remarks>
public void StartTagHelperWritingScope(HtmlEncoder encoder) public void StartTagHelperWritingScope(HtmlEncoder encoder)
{ {
var viewContext = ViewContext;
var buffer = new ViewBuffer(BufferScope, Path, ViewBuffer.TagHelperPageSize); var buffer = new ViewBuffer(BufferScope, Path, ViewBuffer.TagHelperPageSize);
TagHelperScopes.Push(new TagHelperScopeInfo(buffer, HtmlEncoder, ViewContext.Writer)); TagHelperScopes.Push(new TagHelperScopeInfo(buffer, HtmlEncoder, viewContext.Writer));
// If passed an HtmlEncoder, override the property. // If passed an HtmlEncoder, override the property.
if (encoder != null) if (encoder != null)
@ -194,7 +195,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// We need to replace the ViewContext's Writer to ensure that all content (including content written // We need to replace the ViewContext's Writer to ensure that all content (including content written
// from HTML helpers) is redirected. // from HTML helpers) is redirected.
ViewContext.Writer = new ViewBufferTextWriter(buffer, ViewContext.Writer.Encoding); viewContext.Writer = new ViewBufferTextWriter(buffer, viewContext.Writer.Encoding);
} }
/// <summary> /// <summary>
@ -238,7 +239,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor
throw new InvalidOperationException(Resources.RazorPage_NestingAttributeWritingScopesNotSupported); throw new InvalidOperationException(Resources.RazorPage_NestingAttributeWritingScopesNotSupported);
} }
_pageWriter = ViewContext.Writer; var viewContext = ViewContext;
_pageWriter = viewContext.Writer;
if (_valueBuffer == null) if (_valueBuffer == null)
{ {
@ -247,7 +249,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// We need to replace the ViewContext's Writer to ensure that all content (including content written // We need to replace the ViewContext's Writer to ensure that all content (including content written
// from HTML helpers) is redirected. // from HTML helpers) is redirected.
ViewContext.Writer = _valueBuffer; viewContext.Writer = _valueBuffer;
} }
@ -284,15 +286,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor
throw new ArgumentNullException(nameof(writer)); throw new ArgumentNullException(nameof(writer));
} }
_textWriterStack.Push(ViewContext.Writer); var viewContext = ViewContext;
ViewContext.Writer = writer; _textWriterStack.Push(viewContext.Writer);
viewContext.Writer = writer;
} }
// Internal for unit testing. // Internal for unit testing.
protected internal virtual TextWriter PopWriter() protected internal virtual TextWriter PopWriter()
{ {
ViewContext.Writer = _textWriterStack.Pop(); var viewContext = ViewContext;
return ViewContext.Writer; var writer = _textWriterStack.Pop();
viewContext.Writer = writer;
return writer;
} }
public virtual string Href(string contentPath) public virtual string Href(string contentPath)
@ -304,9 +309,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor
if (_urlHelper == null) if (_urlHelper == null)
{ {
var services = ViewContext?.HttpContext.RequestServices; var viewContext = ViewContext;
var services = viewContext?.HttpContext.RequestServices;
var factory = services.GetRequiredService<IUrlHelperFactory>(); var factory = services.GetRequiredService<IUrlHelperFactory>();
_urlHelper = factory.GetUrlHelper(ViewContext); _urlHelper = factory.GetUrlHelper(viewContext);
} }
return _urlHelper.Content(contentPath); return _urlHelper.Content(contentPath);
@ -637,8 +643,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// before <see cref="RazorPageBase.FlushAsync"/> flushes the headers. </remarks> /// before <see cref="RazorPageBase.FlushAsync"/> flushes the headers. </remarks>
public virtual HtmlString SetAntiforgeryCookieAndHeader() public virtual HtmlString SetAntiforgeryCookieAndHeader()
{ {
var antiforgery = ViewContext?.HttpContext.RequestServices.GetRequiredService<IAntiforgery>(); var viewContext = ViewContext;
antiforgery.SetCookieTokenAndHeader(ViewContext?.HttpContext); var antiforgery = viewContext?.HttpContext.RequestServices.GetRequiredService<IAntiforgery>();
antiforgery.SetCookieTokenAndHeader(viewContext?.HttpContext);
return HtmlString.Empty; return HtmlString.Empty;
} }