diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 2bb52ccac5..b07e0a5bac 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Net; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.DependencyInjection; @@ -15,6 +17,8 @@ namespace Microsoft.AspNet.Mvc.Razor private readonly HashSet _renderedSections = new HashSet(StringComparer.OrdinalIgnoreCase); private bool _renderedBody; + public IViewComponentHelper Component { get; private set; } + public ViewContext Context { get; set; } public string Layout { get; set; } @@ -42,7 +46,7 @@ namespace Microsoft.AspNet.Mvc.Razor SectionWriters = new Dictionary(StringComparer.OrdinalIgnoreCase); Context = context; - Url = context.ServiceProvider.GetService(); + InitHelpers(); var contentBuilder = new StringBuilder(1024); using (var bodyWriter = new StringWriter(contentBuilder)) @@ -78,6 +82,21 @@ namespace Microsoft.AspNet.Mvc.Razor } } + private void InitHelpers() + { + Contract.Assert(Context != null); + + Url = Context.ServiceProvider.GetService(); + + Component = Context.ServiceProvider.GetService(); + + var contextable = Component as ICanHasViewContext; + if (contextable != null) + { + contextable.Contextualize(Context); + } + } + private async Task RenderLayoutAsync(ViewContext context, string bodyContent) { var virtualPathFactory = context.ServiceProvider.GetService(); diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs index 232eb6ac58..3cc5af08fe 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs @@ -18,8 +18,6 @@ namespace Microsoft.AspNet.Mvc.Razor public ViewDataDictionary ViewData { get; private set; } - public IViewComponentHelper Component { get; private set; } - public IHtmlHelper Html { get; set; } public override Task RenderAsync([NotNull] ViewContext context) @@ -55,14 +53,6 @@ namespace Microsoft.AspNet.Mvc.Razor { contextable.Contextualize(context); } - - Component = context.ServiceProvider.GetService(); - - contextable = Component as ICanHasViewContext; - if (contextable != null) - { - contextable.Contextualize(context); - } } } }