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.

4
  • 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. Commented Jul 16, 2021 at 11:47
  • @Justthebenno This can't be done at all or can't be done in the way I am suggesting? Commented Jul 16, 2021 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. Commented Jul 16, 2021 at 12:36
  • @Randy. The second. It is possible but not within the client-side built process. Commented Jul 16, 2021 at 16:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.