Execute a JavaScript function in a C# application

Deadly 提交于 2019-12-17 18:59:48

问题


Without using a WebBrowser control, how could I execute a JavaScript function, that is decoupled from any browser DOM dependencies, in a C# application? I am investigating implementing validation rules only once, for both server and client validation. See this question for reference if you like. Are there any JS interpretor libraries I could use?

Addendum: My goal here is to automatically deploy and invoke exactly the same validation logic on client and server, without communication between the two during the actual validation. So, ideally I would code my validation logic in JavaScript, and render that to the page. Alternatively, I could code my logic in C#, convert that to JavaScript using Script#, and render that to the page.

I don't want separate validation functions being maintained client and server side, and I don't want AJAX calls to server validation functions, and I don't want client only or server only validation.


回答1:


This may help A script host for C#/VB.NET/JScript.NET it uses built in classes within the Framework to execute script in VB, C# or JavaScript.

It uses the CodeDomProvider to execute the scripts, also check out this article for a more complex example.




回答2:


You could use JScript.Net

Either that, or investigate Script#




回答3:


Windows Script Host (WSH) is a Windows administration tool. You can use PInvoke to activate it from C#. Recognizes an MS specific (and old) version of JavaScript.

WSH creates an environment for hosting scripts. That is, when a script arrives at your computer, WSH plays the part of the host — it makes objects and services available for the script and provides a set of guidelines within which the script is executed. Among other things, Windows Script Host manages security and invokes the appropriate script engine.




回答4:


Try pinvoke spider monkey - http://www.mozilla.org/js/spidermonkey/



来源:https://stackoverflow.com/questions/268320/execute-a-javascript-function-in-a-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!