1212
1313namespace AspCoreServer . Controllers
1414{
15- public class HomeController : Controller
15+ public class HomeController : Controller
16+ {
17+ [ HttpGet ]
18+ public async Task < IActionResult > Index ( )
1619 {
17- [ HttpGet ]
18- public async Task < IActionResult > Index ( )
19- {
20- var nodeServices = Request . HttpContext . RequestServices . GetRequiredService < INodeServices > ( ) ;
21- var hostEnv = Request . HttpContext . RequestServices . GetRequiredService < IHostingEnvironment > ( ) ;
22-
23- var applicationBasePath = hostEnv . ContentRootPath ;
24- var requestFeature = Request . HttpContext . Features . Get < IHttpRequestFeature > ( ) ;
25- var unencodedPathAndQuery = requestFeature . RawTarget ;
26- var unencodedAbsoluteUrl = $ "{ Request . Scheme } ://{ Request . Host } { unencodedPathAndQuery } ";
27-
28- // ** TransferData concept **
29- // Here we can pass any Custom Data we want !
30-
31- // By default we're passing down Cookies, Headers, Host from the Request object here
32- TransferData transferData = new TransferData ( ) ;
33- transferData . request = AbstractHttpContextRequestInfo ( Request ) ;
34- transferData . thisCameFromDotNET = "Hi Angular it's asp.net :)" ;
35- // Add more customData here, add it to the TransferData class
36-
37- // Prerender / Serialize application (with Universal)
38- var prerenderResult = await Prerenderer . RenderToString (
20+ var nodeServices = Request . HttpContext . RequestServices . GetRequiredService < INodeServices > ( ) ;
21+ var hostEnv = Request . HttpContext . RequestServices . GetRequiredService < IHostingEnvironment > ( ) ;
22+
23+ var applicationBasePath = hostEnv . ContentRootPath ;
24+ var requestFeature = Request . HttpContext . Features . Get < IHttpRequestFeature > ( ) ;
25+ var unencodedPathAndQuery = requestFeature . RawTarget ;
26+ var unencodedAbsoluteUrl = $ "{ Request . Scheme } ://{ Request . Host } { unencodedPathAndQuery } ";
27+
28+ // ** TransferData concept **
29+ // Here we can pass any Custom Data we want !
30+
31+ // By default we're passing down Cookies, Headers, Host from the Request object here
32+ TransferData transferData = new TransferData ( ) ;
33+ transferData . request = AbstractHttpContextRequestInfo ( Request ) ;
34+ transferData . thisCameFromDotNET = "Hi Angular it's asp.net :)" ;
35+ // Add more customData here, add it to the TransferData class
36+
37+ //Prerender now needs CancellationToken
38+ System . Threading . CancellationTokenSource cancelSource = new System . Threading . CancellationTokenSource ( ) ;
39+ System . Threading . CancellationToken cancelToken = cancelSource . Token ;
40+
41+ // Prerender / Serialize application (with Universal)
42+ var prerenderResult = await Prerenderer . RenderToString (
3943 "/" ,
4044 nodeServices ,
45+ cancelToken ,
4146 new JavaScriptModuleExport ( applicationBasePath + "/Client/dist/main-server" ) ,
4247 unencodedAbsoluteUrl ,
4348 unencodedPathAndQuery ,
@@ -46,66 +51,66 @@ public async Task<IActionResult> Index()
4651 Request . PathBase . ToString ( )
4752 ) ;
4853
49- ViewData [ "SpaHtml" ] = prerenderResult . Html ; // our <app> from Angular
50- ViewData [ "Title" ] = prerenderResult . Globals [ "title" ] ; // set our <title> from Angular
51- ViewData [ "Styles" ] = prerenderResult . Globals [ "styles" ] ; // put styles in the correct place
52- ViewData [ "Meta" ] = prerenderResult . Globals [ "meta" ] ; // set our <meta> SEO tags
53- ViewData [ "Links" ] = prerenderResult . Globals [ "links" ] ; // set our <link rel="canonical"> etc SEO tags
54- ViewData [ "TransferData" ] = prerenderResult . Globals [ "transferData" ] ; // our transfer data set to window.TRANSFER_CACHE = {};
55-
56- return View ( ) ;
57- }
58-
59- [ HttpGet ]
60- [ Route ( "sitemap.xml" ) ]
61- public async Task < IActionResult > SitemapXml ( )
62- {
63- String xml = "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>" ;
64-
65- xml += "<sitemapindex xmlns=\" http://www.sitemaps.org/schemas/sitemap/0.9\" >" ;
66- xml += "<sitemap>" ;
67- xml += "<loc>http://localhost:4251/home</loc>" ;
68- xml += "<lastmod>" + DateTime . Now . ToString ( "yyyy-MM-dd" ) + "</lastmod>" ;
69- xml += "</sitemap>" ;
70- xml += "<sitemap>" ;
71- xml += "<loc>http://localhost:4251/counter</loc>" ;
72- xml += "<lastmod>" + DateTime . Now . ToString ( "yyyy-MM-dd" ) + "</lastmod>" ;
73- xml += "</sitemap>" ;
74- xml += "</sitemapindex>" ;
75-
76- return Content ( xml , "text/xml" ) ;
77-
78- }
79-
80- public IActionResult Error ( )
81- {
82- return View ( ) ;
83- }
84-
85- private IRequest AbstractHttpContextRequestInfo ( HttpRequest request )
86- {
87-
88- IRequest requestSimplified = new IRequest ( ) ;
89- requestSimplified . cookies = request . Cookies ;
90- requestSimplified . headers = request . Headers ;
91- requestSimplified . host = request . Host ;
92-
93- return requestSimplified ;
94- }
54+ ViewData [ "SpaHtml" ] = prerenderResult . Html ; // our <app> from Angular
55+ ViewData [ "Title" ] = prerenderResult . Globals [ "title" ] ; // set our <title> from Angular
56+ ViewData [ "Styles" ] = prerenderResult . Globals [ "styles" ] ; // put styles in the correct place
57+ ViewData [ "Meta" ] = prerenderResult . Globals [ "meta" ] ; // set our <meta> SEO tags
58+ ViewData [ "Links" ] = prerenderResult . Globals [ "links" ] ; // set our <link rel="canonical"> etc SEO tags
59+ ViewData [ "TransferData" ] = prerenderResult . Globals [ "transferData" ] ; // our transfer data set to window.TRANSFER_CACHE = {};
60+
61+ return View ( ) ;
9562 }
9663
97- public class IRequest
64+ [ HttpGet ]
65+ [ Route ( "sitemap.xml" ) ]
66+ public async Task < IActionResult > SitemapXml ( )
9867 {
99- public object cookies { get ; set ; }
100- public object headers { get ; set ; }
101- public object host { get ; set ; }
68+ String xml = "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>" ;
69+
70+ xml += "<sitemapindex xmlns=\" http://www.sitemaps.org/schemas/sitemap/0.9\" >" ;
71+ xml += "<sitemap>" ;
72+ xml += "<loc>http://localhost:4251/home</loc>" ;
73+ xml += "<lastmod>" + DateTime . Now . ToString ( "yyyy-MM-dd" ) + "</lastmod>" ;
74+ xml += "</sitemap>" ;
75+ xml += "<sitemap>" ;
76+ xml += "<loc>http://localhost:4251/counter</loc>" ;
77+ xml += "<lastmod>" + DateTime . Now . ToString ( "yyyy-MM-dd" ) + "</lastmod>" ;
78+ xml += "</sitemap>" ;
79+ xml += "</sitemapindex>" ;
80+
81+ return Content ( xml , "text/xml" ) ;
82+
83+ }
84+
85+ public IActionResult Error ( )
86+ {
87+ return View ( ) ;
10288 }
10389
104- public class TransferData
90+ private IRequest AbstractHttpContextRequestInfo ( HttpRequest request )
10591 {
106- public dynamic request { get ; set ; }
10792
108- // Your data here ?
109- public object thisCameFromDotNET { get ; set ; }
93+ IRequest requestSimplified = new IRequest ( ) ;
94+ requestSimplified . cookies = request . Cookies ;
95+ requestSimplified . headers = request . Headers ;
96+ requestSimplified . host = request . Host ;
97+
98+ return requestSimplified ;
11099 }
100+ }
101+
102+ public class IRequest
103+ {
104+ public object cookies { get ; set ; }
105+ public object headers { get ; set ; }
106+ public object host { get ; set ; }
107+ }
108+
109+ public class TransferData
110+ {
111+ public dynamic request { get ; set ; }
112+
113+ // Your data here ?
114+ public object thisCameFromDotNET { get ; set ; }
115+ }
111116}
0 commit comments