1010using System . Security . Principal ;
1111using System . Text ;
1212using System . Threading . Tasks ;
13+ using Microsoft . AspNet . Http ;
1314using Microsoft . AspNet . Mvc . Rendering ;
1415using Microsoft . Framework . DependencyInjection ;
1516
@@ -22,7 +23,20 @@ public abstract class RazorView : IView
2223
2324 public IViewComponentHelper Component { get ; private set ; }
2425
25- public ViewContext Context { get ; set ; }
26+ public HttpContext Context
27+ {
28+ get
29+ {
30+ if ( ViewContext == null )
31+ {
32+ return null ;
33+ }
34+
35+ return ViewContext . HttpContext ;
36+ }
37+ }
38+
39+ public ViewContext ViewContext { get ; set ; }
2640
2741 public string Layout { get ; set ; }
2842
@@ -34,20 +48,20 @@ public virtual IPrincipal User
3448 {
3549 get
3650 {
37- if ( Context == null || Context . HttpContext == null )
51+ if ( Context == null )
3852 {
3953 return null ;
4054 }
4155
42- return Context . HttpContext . User ;
56+ return Context . User ;
4357 }
4458 }
4559
4660 public dynamic ViewBag
4761 {
4862 get
4963 {
50- return ( Context == null ) ? null : Context . ViewBag ;
64+ return ( ViewContext == null ) ? null : ViewContext . ViewBag ;
5165 }
5266 }
5367
@@ -60,7 +74,7 @@ public dynamic ViewBag
6074 public virtual async Task RenderAsync ( [ NotNull ] ViewContext context )
6175 {
6276 SectionWriters = new Dictionary < string , HelperResult > ( StringComparer . OrdinalIgnoreCase ) ;
63- Context = context ;
77+ ViewContext = context ;
6478
6579 InitHelpers ( ) ;
6680
@@ -100,16 +114,17 @@ public virtual async Task RenderAsync([NotNull] ViewContext context)
100114
101115 private void InitHelpers ( )
102116 {
117+ Contract . Assert ( ViewContext != null ) ;
103118 Contract . Assert ( Context != null ) ;
104119
105- Url = Context . HttpContext . RequestServices . GetService < IUrlHelper > ( ) ;
120+ Url = Context . RequestServices . GetService < IUrlHelper > ( ) ;
106121
107- Component = Context . HttpContext . RequestServices . GetService < IViewComponentHelper > ( ) ;
122+ Component = Context . RequestServices . GetService < IViewComponentHelper > ( ) ;
108123
109124 var contextable = Component as ICanHasViewContext ;
110125 if ( contextable != null )
111126 {
112- contextable . Contextualize ( Context ) ;
127+ contextable . Contextualize ( ViewContext ) ;
113128 }
114129 }
115130
0 commit comments