最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - is it possible to execute javascript code in the .net framework? - Stack Overflow

programmeradmin1浏览0评论

i want to be able to execute JavaScript on the server side which is running the environment.
so how is it possible to do so? mainly i need text processing functions, i will input a string and get a string returned from JavaScript code.
no window interaction is needed.

i want to be able to execute JavaScript on the server side which is running the .net environment.
so how is it possible to do so? mainly i need text processing functions, i will input a string and get a string returned from JavaScript code.
no window interaction is needed.

Share Improve this question asked Apr 25, 2012 at 12:54 KarimKarim 6,28319 gold badges60 silver badges83 bronze badges 6
  • 4 .Net is pretty good for text processing. Is there something specific you need that only JavaScript can provide? – Marcelo Cantos Commented Apr 25, 2012 at 12:57
  • I'd second @MarceloCantos' comment. What's the advantage of going outside of .Net for text processing? – KP. Commented Apr 25, 2012 at 13:00
  • 2 This seems like a bizarre thing to want to do. What exactly are you trying to accomplish? – asawyer Commented Apr 25, 2012 at 13:00
  • i need to test JavaScript code that it performs correctly. that is in an automatic way. – Karim Commented Apr 25, 2012 at 13:19
  • There are a host of javascript unit testing frameworks out there. – asawyer Commented Apr 25, 2012 at 22:16
 |  Show 1 more comment

2 Answers 2

Reset to default 13

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

EDIT: Five years later the JS engines native to .NET are somewhat lagging behind (none supports ES6 yet, and IronJS seems abandoned), but we now also have the open-source ChakraCore which is not very hard to integrate and use in .NET with one of the readily available wrappers.

Also, the JavaScriptEngineSwitcher allows using almost any of the existing JS engines from within .NET code through a common interface, so that switching engines does not need changing code.

You can write a JScript.Net file and compile it into an assembly with jsc, then just use that 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
            }
        }
    }
}

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

You then compile that into an assembly like this:

jsc /target:library TheFileName.js

...and it produces and assembly in TheFileName.dll.

发布评论

评论列表(0)

  1. 暂无评论