22using System . Text . Json . Serialization ;
33using System . Web ;
44using Microsoft . AspNetCore . Html ;
5+ using Microsoft . AspNetCore . Http ;
56
67namespace InertiaCore ;
78
@@ -13,14 +14,19 @@ internal interface IResponseFactory
1314 public void Version ( object ? version ) ;
1415 public string ? GetVersion ( ) ;
1516 public LocationResult Location ( string url ) ;
17+ public void Share ( string key , object ? value ) ;
18+ public void Share ( IDictionary < string , object ? > data ) ;
1619}
1720
1821internal class ResponseFactory : IResponseFactory
1922{
20- private string _rootView = "~/Views/App.cshtml" ;
23+ private readonly IHttpContextAccessor _contextAccessor ;
2124
25+ private string _rootView = "~/Views/App.cshtml" ;
2226 private object ? _version ;
2327
28+ public ResponseFactory ( IHttpContextAccessor contextAccessor ) => _contextAccessor = contextAccessor ;
29+
2430 public Response Render ( string component , object ? props = null )
2531 {
2632 props ??= new { } ;
@@ -54,4 +60,26 @@ public IHtmlContent Html(dynamic model)
5460 } ;
5561
5662 public LocationResult Location ( string url ) => new ( url ) ;
63+
64+ public void Share ( string key , object ? value )
65+ {
66+ var context = _contextAccessor . HttpContext ! ;
67+
68+ var sharedData = context . Features . Get < InertiaSharedData > ( ) ;
69+ sharedData ??= new InertiaSharedData ( ) ;
70+ sharedData . Set ( key , value ) ;
71+
72+ context . Features . Set ( sharedData ) ;
73+ }
74+
75+ public void Share ( IDictionary < string , object ? > data )
76+ {
77+ var context = _contextAccessor . HttpContext ! ;
78+
79+ var sharedData = context . Features . Get < InertiaSharedData > ( ) ;
80+ sharedData ??= new InertiaSharedData ( ) ;
81+ sharedData . Merge ( data ) ;
82+
83+ context . Features . Set ( sharedData ) ;
84+ }
5785}
0 commit comments