C# 后台调用前台JS

 

1.需要添加微软的类库  Interop.MSScriptControl.dll

 

2.

var path = Path.GetFullPath("../../javascript/youziku.lib.js");
string str2 = File.ReadAllText(path);
string fun = string.Format(@"hex_md5('{0}')", item.Text);
string result = ExecuteScript(fun, str2);

 

3.

private string ExecuteScript(string sExpression, string sCode)
{
MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
scriptControl.UseSafeSubset = true;
scriptControl.Language = "JScript";
scriptControl.AddCode(sCode);
try
{

string str = scriptControl.Eval(sExpression).ToString();
return str;
}
catch (Exception ex)
{
string str = ex.Message;
}
return null;
}

posted @ 2015-11-16 17:56  逍遥帝君  阅读(998)  评论(0编辑  收藏  举报