Last active
January 29, 2018 13:17
-
-
Save ismcagdas/dfdeba1431f4ac3bf2ed268311a48bbe to your computer and use it in GitHub Desktop.
Acme.HeroShop - HomeController.cs - 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HomeController : Controller | |
| { | |
| private readonly IHostingEnvironment _hostingEnvironment; | |
| public HomeController(IHostingEnvironment hostingEnvironment) | |
| { | |
| _hostingEnvironment = hostingEnvironment; | |
| } | |
| [HttpGet] | |
| public async Task<IActionResult> Index() | |
| { | |
| // for production, enable server side renderring | |
| var applicationBasePath = _hostingEnvironment.ContentRootPath; | |
| var requestFeature = Request.HttpContext.Features.Get<IHttpRequestFeature>(); | |
| var unencodedPathAndQuery = requestFeature.RawTarget; | |
| var unencodedAbsoluteUrl = $"{Request.Scheme}://{Request.Host}{unencodedPathAndQuery}"; | |
| // ** TransferData concept ** | |
| // Here we can pass any Custom Data we want ! | |
| TransferData transferData = new TransferData | |
| { | |
| // By default we're passing down Cookies, Headers, Host from the Request object here | |
| request = AbstractHttpContextRequestInfo(Request) | |
| // ex: | |
| // transferData.thisCameFromDotNET = "Hi Angular it's asp.net :)"; | |
| // Add more customData here, add it to the TransferData class | |
| }; | |
| var nodeService = Request.HttpContext.RequestServices.GetRequiredService<INodeServices>(); // nodeServices | |
| //Prerender now needs CancellationToken | |
| System.Threading.CancellationTokenSource cancelSource = new System.Threading.CancellationTokenSource(); | |
| System.Threading.CancellationToken cancelToken = cancelSource.Token; | |
| // Prerender / Serialize application (with Universal) | |
| var prerenderResult = await Prerenderer.RenderToString( | |
| "/", | |
| nodeService, | |
| //Request.HttpContext.RequestServices.GetRequiredService<INodeServices>(), // nodeServices | |
| cancelToken, | |
| new JavaScriptModuleExport(applicationBasePath + "/HeroApp/dist-server/main.bundle"), | |
| unencodedAbsoluteUrl, | |
| unencodedPathAndQuery, | |
| transferData, // Our simplified Request object & any other CustommData you want to send! | |
| 30000, | |
| Request.PathBase.ToString() | |
| ); | |
| ViewData["SpaHtml"] = prerenderResult.Html; // our <app> from Angular | |
| ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular | |
| ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place | |
| ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags | |
| ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags | |
| ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {}; | |
| return View(); | |
| } | |
| private SimplifiedRequest AbstractHttpContextRequestInfo(HttpRequest request) | |
| { | |
| return new SimplifiedRequest | |
| { | |
| cookies = request.Cookies, | |
| headers = request.Headers, | |
| host = request.Host | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment