Question : Server-Prerendering + passing data from asp.net mvc to angular component #316

Closed
opened 2025-08-09 17:15:49 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @jagmagh on 11/17/2017

What I am trying to do: I would like to pass a value from my asp.net controller to my angular application where it can be used within the various angular components.

What I have done I referred to https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices especially the sections on "Passing data from .NET code into JavaScript code" and "Passing data from server-side to client-side code". Following those examples, at this point, I have my data as a global value in window object. I can see it in console by doing window.data.

Issue :- However, the problem is that I would like to access this global variable within my components .ts file so that I can use it in my component. But since I am using Server side pre-rendering, the pre-rendering (correctly) throws an error if I try do to "window.data" in my .ts file.

The error is

Microsoft.AspNetCore.NodeServices:Error: ERROR ReferenceError: window is not defined
    at NavBarComponent.get [as loggedInDatabase] (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\main-server.js:22810:17)
    at Object.eval [as updateRenderer] (ng:///AppModule/NavBarComponent.ngfactory.js:150:23)
    at Object.updateRenderer (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23356:70)
    at checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22885:14)
    at callViewAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23243:21)
    at execComponentViewsAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23175:13)
    at checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22886:5)
    at callViewAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23243:21)
    at execComponentViewsAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23175:13)
    at Object.checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22886:5)

Is there some workaround or maybe an alternate way to pass data from server to client?

*Originally created by @jagmagh on 11/17/2017* **What I am trying to do:** I would like to pass a value from my asp.net controller to my angular application where it can be used within the various angular components. **What I have done** I referred to [https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices](url) especially the sections on "**Passing data from .NET code into JavaScript code**" and "**Passing data from server-side to client-side code**". Following those examples, at this point, I have my data as a global value in window object. I can see it in console by doing window.data. **Issue :-** However, the problem is that I would like to access this global variable within my components .ts file so that I can use it in my component. But since I am using Server side pre-rendering, the pre-rendering (correctly) throws an error if I try do to "window.data" in my .ts file. The error is ``` Microsoft.AspNetCore.NodeServices:Error: ERROR ReferenceError: window is not defined at NavBarComponent.get [as loggedInDatabase] (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\main-server.js:22810:17) at Object.eval [as updateRenderer] (ng:///AppModule/NavBarComponent.ngfactory.js:150:23) at Object.updateRenderer (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23356:70) at checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22885:14) at callViewAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23243:21) at execComponentViewsAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23175:13) at checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22886:5) at callViewAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23243:21) at execComponentViewsAction (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:23175:13) at Object.checkAndUpdateView (C:\PROJECT\ars.net\ARS.Web\ClientApp\dist\vendor.js:22886:5) ``` Is there some workaround or maybe an alternate way to pass data from server to client?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#316
No description provided.