Is it possible to execute javascript code in .net framework?

I want to be able to execute JavaScript on the server side where the .net environment is running.
so how is this possible? I mainly need text processing functions, I will enter a string and get the string returned from JavaScript code.
no interaction with the window is required.

+6
source share
2 answers

Yes, there are several JS engines that you can use. Jurassic , Jint, and IronJS are .NET-based, but you can also interact with others, such as V8, from the Chrome browser or ActiveScript from IE.

EDIT: Five years later, the JS engines, native to .NET, are slightly behind (none of them support ES6, and IronJS seems abandoned), but now we also have open-source ChakraCore , which is not very difficult to integrate and use in .NET with using one of the easily accessible wrappers.

In addition, JavaScriptEngineSwitcher allows you to use almost any of the existing JS engines from .NET code through a common interface, so that switching engines do not need to change the code.

+11
source

You can write a JScript.Net file and compile it into an assembly with jsc , just use this assembly like any other.

Example:

 package Your.Desired.Package.Name { Class SomeClassName { public static function doSomething(foo) } var bar; // Fairly normal JavaScript code here if (foo.match(/sometest/)) { // Do something } else { // Do something else } } } } 

Besides the package and class structures, the code in JScript.Net is essentially JavaScript. You even have eval if you want to be evil. :-)

Then you compile it into an assembly as follows:

 jsc /target:library TheFileName.js 

... and it produces and compiles in TheFileName.dll .

+3
source

Source: https://habr.com/ru/post/914127/


All Articles