I’m working on an app that uses Angular JS on the client side and ASP.NET MVC on the server side. I have a custom directive written in JavaScript that handles some math operations for the UI. Now I need to create a new API endpoint in my C# controller that can somehow use the same calculation logic from that directive and send back the result.
The tricky part is not making the endpoint itself, but figuring out how to make my C# backend code communicate with the JavaScript directive code. What’s the best way to handle this situation?
Interesting challenge! Why not reimplement the math logic in C# instead of calling the JS directly? What kind of calculations are you dealing with? Are they complex enough that maintaining two versions would be a pain?
i know it seems tough, but js and c# kinda have their own worlds. maybe try putting the math stuff in a shared service? makes life easier instead of forcing them to talk directly.
Extract your math logic into a separate JavaScript file and run it from C# using Node.js with V8. I’ve dealt with this exact problem - needed the same business rules on frontend and backend. Try Microsoft.ClearScript or Jint libraries. They let you run JavaScript directly in your .NET app, so you can call your existing JS functions from C# without rewriting anything. If performance matters more, port everything to C# but keep the JavaScript version as your reference to make sure both give identical results.