Is it possible to have a shared script in ASP.NET that can be used for both client-side and server-side

Currently, I am developing a web application that involves a calculation method which takes a string as input and returns the number of SMS messages. This function is self-contained and does not rely on any external resources to operate.

I need this function to be functional both in the client-side for displaying the message's SMS count to users and in the server-side for calculating the cost associated with the message transmission.

Initially, my plan was to create a C# version of the function for server-side operations and a separate JavaScript version for client-side functionality. But then I had a 'DRY' moment - What if it's possible to execute JavaScript functions on the server-side as well? I'm uncertain about the feasibility of this approach.

So, here's my question: Is it achievable to run a JavaScript function on the server-side, or perhaps from a class library?

Answer №1

Consider approaching this problem from a different perspective. Rather than attempting to execute javascript on the server side (which may not be feasible), why not create a C# function to perform the necessary calculations and then send the result back to the client? The client can trigger this function, perhaps through an ajax call or by passing it in via a viewmodel.

Answer №2

Here is the solution: 1. Implement an API in C# with a fixed number of parameters for calculation purposes. The client side code can then utilize ajax or xmlHttpRequest to interact with this API and handle the response locally. 2. Consider utilizing Node.js for server-side JavaScript, such as using the Node.js sandbox package, or the native Node.js VM module.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to tell if an uploaded file is in UTF-8 or UTF-16 encoding?

I am currently facing an issue on my website where users can upload a txt file containing data, which is then imported into the database. The problem arises when some users upload the data in UTF-8 format, while others choose UTF-16. byte[] fileData = ...

Using JQuery within Angular 4 is a valuable tool for enhancing the functionality

As a newcomer to Angular, I am experimenting with using jQuery alongside Angular 4. In my search for information, I stumbled upon this question on Stack Overflow. Inside the question, there was an example provided that can be found here. However, when att ...

ReactJS: Want to update card design by utilizing the onClick event handler

Currently, I am aware that there will be a need to refactor this code later on to separate things into their own components. However, due to time constraints, I have to wire this up as is at the moment. To achieve this, I utilized array.map() to create car ...

Find the mean of two values entered by the user using JavaScript

I'm sorry for asking such a basic question, but I've been struggling to get this code to work for quite some time now. I'm ashamed to admit how long I've spent trying to figure it out and how many related StackOverflow questions I ...

Incorporating a local JSON file into a JavaScript script element

Currently, I am utilizing AnyChart to create a Choropleth Map showcasing the number of users per country. In the template provided by AnyChart, the code below is used to specify the URL from which the data should be fetched: anychart.onDocumentReady(func ...

Can the color of text be adjusted (to either white or black) based on the background color (in any color and format)?

To achieve a text color that contrasts well with any background, I need to make sure it's either black or white. The background in my app can vary in color and format, so finding the perfect solution has been challenging. Using mix-blend-mode doesn&a ...

Monitoring Twitter bootstrap modal for scrollbar reaching the bottom

Currently, I am working with Twitter Bootstrap modal and facing a challenge in determining how to detect when the scrollbar of the modal reaches the bottom using either JavaScript or jQuery. https://i.stack.imgur.com/0VkqY.png My current approach involve ...

Can you retrieve a reference/pointer to a specific property of an object in JavaScript?

I am looking to generate an HTML <input> element, and then access its value property so I can update the value through that reference: var input = document.createElement('input'); var valueRef = &(input.value); *valueRef = "Hello world!" ...

The given 'FC<ComponentType>' type argument cannot be assigned to the 'ForwardRefRenderFunction<unknown, ComponentType>' parameter type

Currently, I am using react in conjunction with typescript. Within my project, there are two components - one serving as the child and the other as the parent. I am passing a ref to my child component, and within that same child component, I am binding my ...

Sorting data in Javascript can be done efficiently by utilizing the .filter method

Can someone help me identify what I might be doing incorrectly? I have a chained filter under computed that is giving me an error message stating 'product.topic.sort' is not a function. My intention is to use 'select' to provide sortin ...

What is the best approach to dynamically load html table cell data into a table with changing headers in a React application?

Currently, I am in the process of developing a <table> using React version 0.14.6. This table consists of column headers that are dynamically added to the <thead> section of the table: CourseTable.js: import CourseList from './CourseList ...

Utilizing jQuery to manipulate dynamic elements on the DOM

Struggling to dynamically generate HTML using jQuery and facing challenges with adding a nested loop to generate specific target HTML code. The target HTML code is as follows: <div class="daily-featured__video-social"> <ul class="share-tools_ ...

Unpacking JSON data from social media posts

Having trouble parsing a JSON object in C# from Twitter with JObject? Can't seem to pinpoint where the necessary results are located? For instance: Specifically looking for: Avatar URL Twitter name Message The JSON string appears as follows: {" ...

Encountered CS0433 Error: Redundant GuidAttribute found post migration to .NET Core 6

I recently installed VS2022 and decided to upgrade some old projects from .Net 4 to .Net (Core) 6 using the MS Dot Net Upgrade Assistant. Currently, I am working on a basic command line utility, and encountered the CS0433 error related to a duplicate GuidA ...

How to include a file within another file in Node.js

When including one JavaScript file into another, I typically use the following syntax: var userControllerObj = require("../controller/userController"), userController = new userControllerObj.UserGatewayController(); I'm curious if I can u ...

Displaying image alt text on hover using pure JavaScript

Is there a way to hover over images on my webpage and display their alt text using vanilla javascript, without relying on jQuery? Here is the code for my images: <a href="<?php echo $displayData['href']; ?>" class="group"> <d ...

"When testing with an API client, NextJS 13 successfully returns a response, however, the same response

Having trouble getting a clear answer on something really simple. I've created an API route: // /api/test/route.js export async function GET(request, response) { console.log("requested"); return NextResponse.json({ my: "data" ...

Using jQuery.ajax to manage caching and manipulating timestamps

Currently, I am utilizing the jquery.ajax techniques to transmit GET-requests to the server, with caching disabled by setting cache to false. Everything seems to be working fine so far. It appears that jQuery automatically includes a timestamp (likely epo ...

Error encountered: SyntaxError - Missing semicolon before statement in AJAX call while processing JSON data

I am currently in the process of making a cross domain JSONP call utilizing this code snippet: jQuery.ajax({ async: true, url: 'http://mnews.hostoi.com/test.json', dataType: 'jsonp', method: "GET&quo ...

Modify a necessary input value using jQuery or JavaScript

I am looking to update the required value of an input based on a checkbox selection. Here is my current code, any assistance would be appreciated. <input type="checkbox" id="no_land_line" name="no_land_line" value=""> // check this box if no land li ...