在 C# 应用程序中执行 JavaScript 函数 [英] Execute a JavaScript function in a C# application

查看:21
本文介绍了在 C# 应用程序中执行 JavaScript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不使用 WebBrowser 控件,我如何在 C# 应用程序中执行与任何浏览器 DOM 依赖项分离的 JavaScript 函数?我正在调查仅针对服务器和客户端验证实施一次验证规则.如果您愿意,请参阅这个问题以供参考.有没有我可以使用的 JS 解释器库?

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?

附录:我的目标是在客户端和服务器上自动部署和调用完全相同的验证逻辑,在实际验证期间无需两者之间的通信.因此,理想情况下,我会在 JavaScript 中编写验证逻辑,并将其呈现给页面.或者,我可以在 C# 中编写我的逻辑,使用 Script# 将其转换为 JavaScript,然后呈现到页面.

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.

我不希望在客户端和服务器端维护单独的验证函数,我不希望 AJAX 调用服务器验证函数,我不希望仅客户端或仅服务器验证.

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.

推荐答案

这可能有帮助 C#/VB.NET/JScript.NET 的脚本宿主,它使用框架内的内置类来执行 VB、C# 或 JavaScript 中的脚本.

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.

它使用 CodeDomProvider执行脚本,还可以查看 这篇文章提供了一个更复杂的例子.

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

这篇关于在 C# 应用程序中执行 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆