javascriptasp.net-corereactjs.net

How to read the Asp.Net Core Assembly Version from Javascript


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.


Solution

  • 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.