0

I have a Asp.Net Core App. It uses ReactJS. In ./ClientApp/public I have a JS named configuration.js which contains:

var configs = {
    "apiUrl": "https://localhost:44356"
}

... then in index.html in the body section:

<body>
<noscript>
    You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script src="configuration.js"></script>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>

... (I import this in webpack.config.js) so now react can use the Config.apiUrl. I would like to add a version variable in configuration.js. And read the .NET assembly version in javascript with something like DotNet.invokeMethod from Call .NET methods from JavaScript functions in ASP.NET Core Blazor. Any help would be appreciated.

Randy
  • 1,137
  • 16
  • 49
  • The ``DotNet.invokeMethod`` is from Blazor and can't be used in React. Besides, I think you might confuse different concepts here. I guess ASP.NET core is used for hosting your application - serving the requested js files for react to run. So, if you build react, it is built without dotnet being involved. Sure, you could pass a variable into the build process, but this would only reflect the .net version of the built agent, not the host. If you want to display the version of the server, you would need to create an API endpoint that is then called by your react app. – Just the benno Jul 16 '21 at 11:47
  • @Justthebenno This can't be done at all or can't be done in the way I am suggesting? – Randy Jul 16 '21 at 12:09
  • I agree with @Justthebenno, you will need to create an API endpoint for this to retrieve your .NET assembly version. I've done something similar in the past. – 4e 69 63 6b Jul 16 '21 at 12:36
  • @Randy. The second. It is possible but not within the client-side built process. – Just the benno Jul 16 '21 at 16:05

1 Answers1

0

Giving up on getting the Assembly Version...

After reading SO question Get version number from package.json in React Redux (create-react-app), I added REACT_APP_VERSION=$npm_package_version to my .env. Now, in my React component I have access to process.env.REACT_APP_VERSION. I bump my version with npm version patch, npm version minor or npm version major. Problem solved.

Randy
  • 1,137
  • 16
  • 49