1

I need to evaluate Javascript code from .netcore 2.1 project

    string vRule = "var input = arguments[0]; if (!input) return \"\"; if(input.length != 7) return \"a fixed string length of 7 is required\"; else return \"\"";
    string spResponse = "sdfsd23";

    string errorText =
        _jscriptEval.EvalToString(new List<object>
            {
                "var args = new Array('" + spResponse +
                "');\r\n validateRule(args);\r\n function validateRule(arguments){" + vRule +
                "}\r\n"
            });
2
  • Hi, @Pratik Bhoir, please provide the logic of your java script codes. From your codes, it's hard to understand and then we could try to transform for u. Commented Sep 1, 2020 at 9:45
  • The Jint Packaged worked properly as expected. Commented Sep 2, 2020 at 11:18

1 Answer 1

8
+100

You can use Jint. Jint implements the ECMA 5.1 spec and can be use from any .NET implementation (Xamarin, .NET Framework, .NET Core). Just use the NuGet package and has no dependencies to other stuff - it’s a single .dll and you are done! Just transform your javascript code into a function and run it like this (this is just an example, not your implementation):

  var engine = new Engine()
    .Execute("function MyFunction(a, b) { return a + b; }")
    ;

engine.Invoke("MyFunction", 1, 2); // -> 3

You have more explanations and examples on https://github.com/sebastienros/jint

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @d-a , I was searching for some similar package was not able to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.