.net core 使用js,.net core 使用javascript,在.net core项目中怎么使用javascript
我项目里需要用到“文字编码”,为了保证前端和后端的编码解码不处bug,
所以,我在项目中用了这个
下面推荐之前在.net F4.0时的方法
文章一:
c# 解码 js的escape_c# escape-CSDN博客
文章二:
c#对js的encodeURI() 编码 decodeURI()解码 escape() 编码unescape()解码,decodeURIComponent() ,encodeURICompon加密解密_c# encodeuricomponent-CSDN博客
管理Nuget程序包
选中项目》依赖项》右键》管理Nuget程序包》浏览》
搜索:Microsoft.ClearScript
Microsoft.ClearScript
Microsoft.ClearScript介绍
地址:NuGet Gallery | Microsoft.ClearScript 7.4.5
项目地址:GitHub - microsoft/ClearScript: A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
API文档:
ClearScript Library Reference
使用说明:
ClearScript FAQtorial
使用说明
源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;namespace TestCode
{public class ClearScriptCore{public static void Exe(){using (var engine = new V8ScriptEngine()){string str = "你好";string jsFunName = "escape";string result = engine.Invoke(jsFunName, str).ToString(); //只能调用全局方法Console.WriteLine($"str:{str}\n\r jsFunName:{jsFunName}\n\r result:{result}");str = result;jsFunName = "unescape";string result2 = engine.Invoke(jsFunName, str).ToString();Console.WriteLine($"str:{str}\n\r jsFunName:{jsFunName}\n\r result2:{result2}");}}}
}
增加引用
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
代码实现和测试
using (var engine = new V8ScriptEngine())
{string str = "你好";string jsFunName = "escape";string result = engine.Invoke(jsFunName, str).ToString(); //只能调用全局方法Console.WriteLine($"str:{str}\n\r jsFunName:{jsFunName}\n\r result:{result}");str = result;jsFunName = "unescape";string result2 = engine.Invoke(jsFunName, str).ToString();Console.WriteLine($"str:{str}\n\r jsFunName:{jsFunName}\n\r result2:{result2}");
}
运行结果
str:你好jsFunName:escaperesult:%u4F60%u597Dstr:%u4F60%u597DjsFunName:unescaperesult2:你好
代码封装
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JinianNet.JNTemplate;
using Microsoft.ClearScript.V8;namespace JJY.Cheng
{public class JavaScriptHelper{/// <summary>/// escape编码/// </summary>/// <param name="val"></param>/// <returns></returns>public static string escape(string val){string result = "";if (string.IsNullOrWhiteSpace(val)){result = "";}else{using (var engine = new V8ScriptEngine()){ result = engine.Invoke("escape", val).ToString();}} return result;}/// <summary>/// unescape解码/// </summary>/// <param name="val"></param>/// <returns></returns>public static string unescape(string val){string result = "";if (string.IsNullOrWhiteSpace(val)){result = "";}else{using (var engine = new V8ScriptEngine()){result = engine.Invoke("unescape", val).ToString();}}return result;}}
}
延伸阅读
C# / .NET Core 调用javascript方法(适用于Windows/Linux平台) 有很好的示例代码
下面内容来自:
https://www.cnblogs.com/shanyou/p/13837007.html
引入Nuget包,Microsoft.ClearScript
demo.js(原生javascript)
function m(t, e, r) {p(v(t))
}
引入Nuget包,Microsoft.ClearScript
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
初始化
using (var engine = new V8ScriptEngine())
{engine.DocumentSettings.AccessFlags = Microsoft.ClearScript.DocumentAccessFlags.EnableFileLoading;engine.DefaultAccess = Microsoft.ClearScript.ScriptAccess.Full; // 这两行是为了允许加载js文件// do something
}
调用脚本有多种方案。
方案一:
调用engine.ComplieDocument方法直接加载js文件,然后调用engine.Execute将引入的脚本执行一遍,这样后面就可以调用js方法,m就是js的方法名,调用格式与js相同。
V8Script script = engine.CompileDocument(ScriptFilePath); // 载入并编译js文件, 然后Execute, 就可以直接调用。
engine.Execute(script);
var result = engine.Script.m("SHAURCOnewayduew&^%5d54nc'KH");
方案二:
将要导入的js方法的代码读出来,然后执行一遍,再调用要执行的js方法
string scriptContent = string.Empty;
using(FileStream fs = new FileStream(ScriptFilePath, FileMode.Open, FileAccess.Read))
{using(StreamReader sr = new StreamReader(fs)){scriptContent = sr.ReadToEnd().Replace("\r\n", "");}
}
engine.Execute(scriptContent); // 取得脚本里的所有内容,Execute一下,然后,调用engine.Script.func(x,y)执行一下。var result = engine.Script.m("SHAURCOnewayduew&^%5d54nc'KH");
直接调用执行
直接调用执行调用的方法的js代码也是可以的
string scriptContent = string.Empty;
using(FileStream fs = new FileStream(ScriptFilePath, FileMode.Open, FileAccess.Read))
{using(StreamReader sr = new StreamReader(fs)){scriptContent = sr.ReadToEnd().Replace("\r\n", "");}
}
scriptContent += "m(\"SHAURCOnewayduew&^%5d54nc'KH\");"; // 在js代码的结尾加上执行的代码engine.Execute(scriptContent); // 取得脚本里的所有内容,Execute一下,然后,调用engine.Script.func(x,y)执行一下。var result = engine.Script.m("SHAURCOnewayduew&^%5d54nc'KH");
调用js的默认方法
特殊情况,调用js全局方法,就是调用js的默认的那些方法
var result = engine.Invoke("encodeURIComponent", "SHAURCOnewayduew&^%5d54nc'KH"); //只能调用全局方法,如encodeURIComponen
上文中用的就是这个方法